2012-05-02 118 views
1

我有這段代碼可以讓我創建一個自定義的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方法做到如此簡單。就我而言,情況完全不同。

建議,請:)

回答

2

您可以設定目標自我和行動,你didPressDone選擇:

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

的選擇應該有這樣的簽名:

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

好的!會嘗試這一個:) – Grauzten

+0

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

0

怎麼樣,這 -

第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]; 
} 

希望,這可能有所幫助。