2015-06-24 168 views
-1

我已經創建了一個主視圖控制器的底部向上滑動視圖控制器,但我不知道如何將信息從主視圖控制器傳遞到向上滑動的控制器。我在主視圖控制器中有一個按鈕,可以顯示或隱藏滑動視圖控制器,並且我想使用按鈕傳遞信息。下面的代碼是按鈕的IBAction,它會顯示/隱藏幻燈片視圖控制器,但沒有一個segue我不知道如何傳遞任何東西。如何將變量從一個視圖控制器傳遞到另一個視圖控制器?

- (IBAction)btnMoveToShowBottomView:(id)sender { 
    UIButton *button = sender; 

    switch (button.tag) { 
     case 0: 
      [_delegate moveToHideBottomTableView]; 
      break; 

     case 1: 
      BottomTableViewController *tableScreen = [[BottomTableViewController alloc]...]; 
     tableScreen.photoDesc = selectedPhotoDesc; 
     [_delegate moveToShowBottomTableView]; 
      break; 

     default: 
      break; 
    } 
} 

在上面的代碼我試圖訪問上滑視圖控制器(帶有一個名爲BottomTableViewController類),但似乎是一個問題,因爲它是要求預期的表現,當我試圖訪問該類中的一個對象(名爲photoDesc的NSString)。

+0

什麼是'[_delegate moveToShowBottomTableView]'呢?如果你試圖提出一個沒有segue的新視圖控制器,你應該調用'presentViewController'(委託人不應該處理這個)。無論如何,你聽到的錯誤聽起來像是不知道你從哪裏得到'selectedPhotoDesc'。 – Connor

+0

[視圖控制器之間傳遞數據]的可能重複(http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – JAL

回答

0

您可以使用NSNotification機制:

在具有獲得PARAM寄存器通知控制器:

-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [[NSNotificaitonCenter defaultCenter] addObserver: self selector:@selector(handleParam:) name:@"myNotification" object: nil]; 
} 
- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
- (void)handleParam:(NSNotification*)notification 
{ 
    if ([notification.name isEqualToString:@"myNotification") 
    { 
     id myParam = notification.object; 
    } 
} 

在具有發送參數的控制器:

[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object: myObject]; 
相關問題