使用自定义的UIBarButton执行push segue

问题描述:

我有这段代码可以让我创建一个自定义的UIBarButton,它位于我的viewDidLoad中。使用自定义的UIBarButton执行push segue

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                   style:UIBarButtonItemStyleDone 
                   target:nil 
                   action:nil]; 
    self.navigationItem.rightBarButtonItem = doneButton; 

现在,我想下面这个方法当按下UIBarButton使得SEGUE到下一个视图控制器工作将被调用。

- (void) didPressDone { 

    PointsResultsViewController *pointsResults = [self.storyboard instantiateViewControllerWithIdentifier:@"resultsPointsVC"]; 

    [self.navigationController pushViewController:pointsResults animated:YES]; 
} 

我已经对此做了一些研究。然而,我所看到的都是关于从对象库中拖拽一个UIBarButton到ViewController,然后把它连接到下一个VC,让prepareForSegue方法做到如此简单。就我而言,情况完全不同。

建议,请:)

您可以设定目标自我和行动,你didPressDone选择:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                   style:UIBarButtonItemStyleDone 
                   target:self 
                   action:@selector(didPressDone:)]; 

的选择应该有这样的签名:

- (void)didPressDone:(id)sender 
+0

好的!会尝试这一个:) – Grauzten

+0

感谢兄弟!你是男人! :) – Grauzten

怎么样,这 -

第1步:

在你Viewcontroller.h文件,只需创建UIBarButton的IOBOutlet喜欢这个 -

@property(nonatomic, strong) IBOutlet UIBarButton *doneButton; 

和IT-

-(IBAction)didPressDone:(id)sender; 

步骤2的IBAction为方法:

线了通过故事板的UIBarButton,并附加操作方法。

第3步:

现在让我们去Viewcontroller.m文件,并在-(void)viewDidLoad方法,初始化UIBarButton的方式,你wanted-

self.doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                   style:UIBarButtonItemStyleDone 
                   target:nil 
                   action:nil]; 
    self.navigationItem.rightBarButtonItem = self.doneButton; 

除了方法签名,在“ didPressDone“方法实现将相同 -

-(IBAction)didPressDone:(id)sender { 

    PointsResultsViewController *pointsResults = [self.storyboard instantiateViewControllerWithIdentifier:@"resultsPointsVC"]; 

    [self.navigationController pushViewController:pointsResults animated:YES]; 
} 

希望,这可能有所帮助。