2016-02-08 82 views
1

我知道這個問題已經回答過,但我在這些之間有困難。在一個頁面上有一個文本框,我想通過點擊按鈕來檢索它的內容到位於下一頁的標籤中。將數據從一個頁面提取到另一個

我試過這個。 tfName是文本框的ID在1

-(IBAction)onClick:(id)sender 
{ 
NSString *string1 =tfName.text; 
} 

頁和第2頁中

- (void)viewDidLoad { 
    [super viewDidLoad]; 
label1.text=string1; 
} 
+0

你有使用故事板嗎? –

+0

您是使用故事板還是xib –

+0

我正在使用.xib –

回答

1

這裏是簡單的例子的代碼:

類A.H:

UITextfield *tfName; 

類A.M:

tfName.text = @"Hello world"; 




-(IBAction)onClick:(id)sender 
{ 
    ClassB *b = [[ClassB alloc][email protected]"ClassB"]; 
    b.string1 =tfName.text; 
    [self.navigationController pushViewController:b animated:YES]; 
} 

類B.h:

@Property(..)NSString *string1; 

類B.m:

label1.text = _string1. 

注意:您需要導入ClassB的在ClassA的,即#import "ClassB.h"

希望這有助於。

+0

謝謝..它的工作原理 –

0

首先創建視圖控制器對象,然後將值分配給該屬性。

-(IBAction)onClick:(id)sender 
    { 
    SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
     tempView. string1 = tfName.text; 
     [self.navigationController pushViewController:tempView animated:YES]; 

    } 

SecondViewController.h創建像

@Property(nonatomic, Strong)NSString *string1; 

您的實現SecondViewController.m的一個全局字符串systensize你的字符串像

@Synthesize string1; 

最後調用像

- (void)viewDidLoad { 
[super viewDidLoad]; 
if (string1.length >0) 
label1.text=string1; 
} 
+0

你怎麼能通過彈出視圖控制器中的數據,是否有可能直接傳遞... –

+0

對不起,它的pushviewcontroller不popViewController –

+0

你在哪裏創建string1變量是在這個頁面或調用頁面。 –

0

做一個Singleton類

class DataModel { 

class var sharedInstance: DataModel { 
    get { 
     struct Static { 
      static var instance: DataModel? = nil 
      static var token: dispatch_once_t = 0 
     } 
     dispatch_once(&Static.token, { 
      Static.instance = DataModel() 
     }) 
     return Static.instance! 
    } 
} 

var textData:String = "" 

}

按鈕自來水做到這一點:

DataModel.sharedInstance.textData = "page 1 data" 

在第2頁的viewDidLoad基金

label1.text = DataModel.sharedInstance.textData 

這應該做工精細!

相關問題