通用/中央popViewControllerAnimated方法

时间:2013-03-13 12:00:17

标签: ios objective-c generics uinavigationcontroller popviewcontrolleranimated

我有一个很小的问题和场景:

问题:我可以拥有[self.navigationController popViewControllerAnimated:True]

从一个中心阶层控制,必须在每个视图中重复。

所以这就是我到目前为止所做的: 观点:

//this is all I want to accomplish and repeat in every view. I want to pass on the own NavigationController and have central class add an action (and style) to the leftBarButton

[self.navigationItem setLeftBarButtonItem:[actionsCollection setGenericBackButton:self.navigationController]];

中心阶层:

@interface ActionsCollection ()
@property(nonatomic, weak)UINavigationController *NavCtrl; // I do this since I dont seem to be able to pass the self.NavigationController on to the buttons action selector (se bellow).
@end

setGenericBackButton:

-(UIBarButtonItem *)setGenericBackButton:(UINavigationController *)NavContrl{
     _NavCtrl = NavContrl; // so that I can get a the global (to the class) UINavigationController being reached from -(IBAction)backButtonAction (see bellow)

     UIButton* myBackButton = [ButtonMaker BtnTypeBack]; //just returns a styled btn

//this is where it starts to fail - I want to pass the 'NavContrl' on with the selector bunt havnt figured out how (thats why the global variable '_NavContrl'
    [myBackButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem* BackBtnItem = [[UIBarButtonItem alloc]initWithCustomView:myBackButton];

}

backButtonAction确实:

-(IBAction)backButtonAction{
     [_NavCtrl popViewControllerAnimated:TRUE];
}

我是否懒得试图保存代码行以尝试做不可能的事情,或者这实际上可以做到吗? 我很乐意更好地解释这一点。

1 个答案:

答案 0 :(得分:0)

优秀的程序员很懒惰。

也就是说,如果你正确设置了UINavigationController,它会添加自己的后退按钮并为你做这个。您不应该需要[self.navigationController pushViewController:someViewController animated:YES];

之外的任何代码