2010-07-24 90 views
1

之間的文本我做了2次 ,我想在主視圖發送標籤的文本子視圖 一個想它打印在另一個標籤的文本價值.... 如何傳遞文字如何通過意見

回答

2

我不會用一個單例模式或其他任何 '全局變量'。這會使您的視圖控制器非常緊密地耦合並限制可重用性。我只是在第二個視圖控制器中創建一個實例變量,並在呈現視圖之前將其設置在主視圖中。

第二個視圖控制器然後將label.text設置爲(例如)viewDidLoad中的實例變量。

這樣,第二個視圖控制器不依賴於任何「全局變量」或包含,並將更加可重用。

//SecondViewController.h 
@interface SecondViewController : UIViewController { 
    NSString *theLabel; 
} 

@property(nonatomic, copy) NSString *theLabel; //Synthesize in implementation 

@end 

然後在主視圖控制器:

//Create instance of secondViewController 
instanceOfSecondViewController.theLabel = @"Nut"; 
//Present the second view here 
+0

每當有人使用全局變量或在不需要一個單身,我看到別人誰不知道他們在做什麼見。 – lucius 2010-07-24 17:04:11

0

如果類A處理您的廠景和B類手柄視圖2,然後定義類B中的接口來接受新的標籤,以您的UI元素之一,那麼調用從類A.該接口

0

查找到辛格爾頓模式。

What should my Objective-C singleton look like?

然後,你可以這樣做:

//view1 
#import "SingletonClass.h" 
... 
[SingletonClass sharedInstance].savedText = @"blah"; 

//view2 
#import "SingletonClass.h" 
... 
lbl.text = [SingletonClass sharedInstance].savedText;