0

I have a UITableViewController that I pragmatically call into my superview when needed. When I tap a table view I want the info to be placed in a UITextField. now I can get it to log correctly from the superview but the text never gets placed in its correct field.

Coming from the UITablViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    NSLog(@"Index %i Touched:",indexPath.row);

    self.MainView = [[TwitterViewController alloc] init];
    self.MainView.view = super.view;

    selectedFriend = [(Tweet*)[friends objectAtIndex:indexPath.row]followingScreenName];

    self.MainView.selectedFriendForTweet = self.selectedFriend;

    [self.MainView setSelectedFriendInBody:self.selectedFriend 
                                      into:self.MainView.TweetBody 
                                      with:self];

    //NSLog(@"selected name on tblview = %@",selectedFriend);
    self.MainView.TweetBody.text = self.selectedFriend;
}

as you see my method is called when a user taps the tblview

[self.MainView setSelectedFriendInBody:self.selectedFriend into:self.MainView.TweetBody with:self];

Here is that method : Now this log Works and the info is correct but just will not go into the textview!

-(void)setSelectedFriendInBody:(NSString*)aString into:(UITextView*)atextView with:(id)sender
{
    aString = friendsTbl.selectedFriend;
    friendsTbl.selectedFriend = self.selectedFriendForTweet;
    atextView = self.TweetBody;

    [self.TweetBody setText:selectedFriendForTweet];   

    NSLog(@"superviews name = %@", selectedFriendForTweet);
    [selectedFriendForTweet retain];
}

Any Help would be greatly appreciated! Thank you

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
FreeAppl3
  • 858
  • 1
  • 15
  • 32

1 Answers1

1

you are doing some strange stuff in your code. Just an example: your method setSelectedFriendInBody:into: gets two parameters which your are not using in your implementation. Even worse: you are assinging some values to that parameters which has definatly no effect. There has to be sth wrong... your code does sth like this:
a=2; b=3; c=10; d=20;
=> f(x,y)=c+d => f(a,b)=30

And it is a bad idea (with respect to reuseability) to force that the superview is a special view. The right way to do this is to use the delegate-pattern. just define a protocol which should contain your method setSelectedFriendInBody:into: and implement that protocol in your MainView. The TablView only get and call a delegate (an id which implements the protocol).

@protocol MyTablViewDelegate
-(void)setSelectedFriendInBody:(NSString*)aString;
@end

@interface MyTablView : UITableView<UITableViewDelegate> 
{
    id<MyTablViewDelegate> myDelegate;
}
@property (assign) id<MyTablViewDelegate> myDelegate; //I use assign here for avoiding mem-leaks due to circular retain-chains
@end

@implementation MyTablView 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     ....
     NSString *someString = [(Tweet*)[friends objectAtIndex:indexPath.row]followingScreenName];
     NSLog(@"someString: %@", someString); // maybe there is an error in getting that object from array or in followingScreenName

     [self.myDelegate setSelectedFriendInBody: someString];

}
@end


@interface MainView : UIView<MyTablViewDelegate>
...
@end

@implementation MainView
...
-(void) sthAtLoadTime
{
    MyTablView *mySubView = [[MyTablView alloc] init...];
    mySubView.myDelegate = self;
}
-(void)setSelectedFriendInBody:(NSString*)aString
{
    if(!self.TweetBody)
        NSLog(@"ERR: TweetBody not set");
    self.TeetBody.text = aString;

}
@end

Note another thing: I assume that your myTablView also implements the UITableViewDelegate which is also not the best way to do

this should do the work.

thomas
  • 5,637
  • 2
  • 24
  • 35
  • thank you for the response! I am trying this out right now and it is not sending the info through the delegate. Everything works as far as delegates go and able to use the void statement from the Main View but it never gets called when the tableview is selected. this is what gets 2011-09-24 19:02:05.278 ThemeCatcher[11717:607] someString: AboutManagement 2011-09-24 19:02:06.396 ThemeCatcher[11717:607] Index 10 Touched: 2011-09-24 19:02:06.398 ThemeCatcher[11717:607] someString: Ech0riginal 2011-09-24 19:02:07.866 ThemeCatcher[11717:607] Index 12 Touched: – FreeAppl3 Sep 25 '11 at 00:04
  • have you set the delegate to the implementation? I mean the line `mySubView.myDelegate = self;` and is that code-snippet called? and are you sure that the delegate-method is not fired? Please test that with more NSLogs or Breakpoints. – thomas Sep 25 '11 at 00:08
  • Yes just double checked with breakpoints and logs. everything seems to work until the actual delegate methods needs to be called! The NSLog(@"ERR: TweetBody not set"); Never happens! I place logs and breakpoints there and it is never called? I get a breakpoint on the [self.delegate setSelectedFriendInBody: selectedFriend]; but not in the superview void delegate method – FreeAppl3 Sep 25 '11 at 00:22
  • ok, then check that the delegate is really set. Write the following in `didSelectRowAtIndexPath:`: `if(!self.myDelegate) NSLog(@"Delegate not set!!"); else if(! [self.myDelegate respondsToSelector:@selector(setSelectedFriendInBody:)]) NSLog(@"The delegate does not implement the protocol");` The problem is that this concept is working fine. If it doesnt work then there is a problem somewhere else. – thomas Sep 25 '11 at 08:00
  • This logs the delegate is not set so I will continue to see why thank you for all the help! – FreeAppl3 Sep 26 '11 at 04:32
  • be sure that `mySubView.myDelegate = self;` gets called on loading (either init or viewDidLoad) – thomas Sep 26 '11 at 08:23