Domanda

Here is what I wish to do:

There is a parent ViewController. On parent's load, ChildVC is called which contains a UITableView.

Now on childView's table's didSelectRowAtIndexPath method, I wish to pass the selected cell content to parentView. And from parentView I wish to load a second viewController.

How do I do this by delegate?

Edited:

Child :

    @class ViewController;
@protocol CustomViewControllerDelegate;
@interface CustomViewController : UITableViewController
{
    AppDelegate *appDelegate;
     id<CustomViewControllerDelegate> delegate;
}

@property(nonatomic, weak) id<CustomViewControllerDelegate> delegate;

@end

@protocol CustomViewControllerDelegate

-(void)didSelectRow:(NSString *)str;

@end

parent

    @class CustomViewController;
@interface ViewController : UIViewController<CustomViewControllerDelegate> // i get the error here
{
    AppDelegate *appDelegate;

}
È stato utile?

Soluzione

On ChildVC declare a protocol:

@protocol ChildVCDelegate
- (void)didSelectCell:(SomeContent *)content;
@end

Also add the delegate:

@property (nonatomic, weak) id<ChildVCDelegate> delegate;

In the implementation of didSelectRowAtIndexPath, add:

if (_delegate) {
    [_delegate didSelectCell:someContent];
}

In the parent view controller implement didSelectCell and when it creates/shows the ChildVC, set it as the delegate.

I'm not sure what kind of content you want to pass to the parent, so I represented it as SomeContent, but it can be whatever you want to pass.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top