Search code examples
iphoneuitableviewuitextviewsuperview

Sending info from UITableView to SuperView


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


Solution

  • 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.