2012-08-10 138 views
2

我有2個ViewControllers,在1st - TableView和2nd - 按鈕上有標籤。當我點擊在第二的ViewController的按鈕,我需要回去上的TableView,並設置在從第二個ViewController到第一個ViewController

cell.detailTextLabel.text 

文本從標籤上的按鈕。

對於回到第一種觀點我用:

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]; 

,但我怎麼可以從第二視圖設置標籤:在第一種觀點

cell.detailTextLabel.text 

?????

+0

你無法得到通知。改爲使用'viewWillAppear'方法。 – Martol1ni 2012-08-10 10:57:07

+0

即使在viewWillAppear中,您也無法執行它,而無需執行協議 – 2012-08-10 12:53:10

+0

@TeodorCarstea雖然這基本上是真的,但主要的回收信息是,當活動視圖控制器要觸發視圖中已收到'viewDidDisappear ',無論您用於更新的機制是什麼,都不應直接更新非活動視圖(該視圖可能在發生'didReceiveMemoryWarning'時發佈)。您必須更新您的模型,並且只有當另一個視圖控制器變爲活動狀態並且接收到'viewWillAppear'時,才應該執行UI更改。 – Rob 2012-08-11 01:11:47

回答

2

當按鈕被點擊我將在第二視圖控制器定義一個協議&委託

@protocol SecondViewController; 

@interface SecondViewController : UIViewController 

@property (nonatomic, assign) id<SecondViewController> delegate; 

@end 


@protocol SecondViewController <NSObject> 
- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button; 
@end 

然後調用該委託:

- (IBAction)buttonTapped:(UIButton *)sender 
{ 
    // do somthing.. 

    // then tell the delegate about the button tapped 
    [self.delegate secondViewController:self didTappedOnButton:sender]; 
} 

在您的第一視圖控制器實現協議

@interface FirstViewController : UIViewController <SecondViewControllerDelegate> 

當您按下第二個視圖控制器時,設置第一作爲第二委託:

- (void)someMethodThatPushTheSecondViewController 
{ 
    SecondViewController *svc = [[SecondViewController alloc] init]; 
    [self.navigationController pushViewController:svc animated:YES]; 
    svc.delegate = self; 
} 

並實現委託方法時,按鍵敲擊

- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button 
{ 
    // do somthing after button tapped 
    // you can get the button title from button.titleLabel.text 
} 
+0

哇!謝謝你的詳細回答!)))))你可以給我你的Skype嗎?)這不是關於通信的問題) – rubik 2012-08-10 12:22:27

0

要訪問父類的方法或屬性,你必須實現一個協議,並使用它的委託。您可以使用您在當前(父級)類中創建的類對象來訪問子類方法/屬性。但是,您希望如何從子類訪問父類實體?是的,執行協議。

或者新手方法:點擊按鈕後,將需要的值保存到NSUserDefaults中。然後,當你到你的父類(viewController 1),離開viewWillAppear,檢查保存的值,如果它不是零,則顯示它。

0
[self.navigationController popViewControllerAnimated:YES]; 
相關問題