简体   繁体   中英

How to push UIViewController into UINavigationController from seperate UIView of different UIViewController which uses this view

I have a UIViewController ( AbcViewController ) with NavigationController . AbcViewController uses UIView ( AbcView ) as its view. AbcView has a UITableView . I have set this TableView 's datasource in AbcViewController and delegate in its SuperView ie AbcView . How will I push another UIViewController ( XyzViewcontroller ) into navigationController when I select a row in this table because it gives

"error: request for member 'navigationController' in something not a structure or union"

when I do this:

[self.navigationController pushViewController:xyzViewcontroller animated:TRUE]; 

I also did this:

AbcViewController *parentVC;
[parentVC.navigationController pushViewController:xyzViewcontroller animated:TRUE]; 

Though it builds successfully, XyzViewcontroller 's view does not appear. It does not push XyzViewcontroller 's view onto navigationController.

We can make a delegate on AbcView so that we can refer back to the view's viewController which in this case is AbcViewController . We can set delegate as:

@interface AbcView : UIView

{ 
    id delegate;
}

@property(nonatomic, assign) id delegate;

Then in AbcViewController , set the view's delegate like this:

[abcView setDelegate:self];

Now in table method using our delegate from within AbcView as:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   [delegate pushOtherView];

}

it will call - (void)pushOtherView method in AbcViewController whose definition is:

- (void)pushOtherView 
{   
    XyzViewcontroller * xyzViewcontroller = [[[XyzViewcontroller alloc] init] autorelease];
   [self.navigationController pushViewController:xyzViewcontroller animated:YES];   
}

But before using this method, it should be mentioned in protocols otherwise it will show

Warniing:

-pushOtherView' not found in protocol(s).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM