2016-11-07 57 views
0

我有一種情況,我需要發送一個按鈕的textfield值在第一視圖控制器點擊第二視圖控制器和顯示在tableview中,但沒有任何顯示在第二視圖控制器與DIS代碼如何將textfield值發送到另一個視圖控制器中的tableview

viewcontroller.h 
@interface ViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UITextField *textfiled; 

@end 

viewcontroller.m 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    NextViewController*nxt=segue.destinationViewController;  
    nxt.sr=_textfiled.text; 

    } 

NextviewController.h 

@interface NextViewController : UIViewController 
@property (strong,nonatomic)NSMutableArray*arr; 
@property (strong, nonatomic) IBOutlet UITableView *table; 
@property(strong,nonatomic) NSString*sr; 

Nextviewcontroller.m 

@interface NextViewController()<UITableViewDataSource> 

//@property(strong,nonatomic)NSMutableArray*finalarr; 
@end 

@implementation NextViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [_arr addObject:_sr]; 
    [_table reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return _arr.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
      UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
      cell.textLabel.text=[_arr objectAtIndex:indexPath.row]; 
      return cell; 
} 
+0

使用斷點來停止每個重要步驟的進程(準備for segue,viewdidload,cellforrow)。 然後看看你的字符串在哪裏設置,以及它在哪裏讀取。您會很容易地找到數據丟失的地方,並且您將能夠進行調查。這是一個非常非常簡單的問題,你不應該在stackoverflow上詢問這個問題。 –

回答

0

你可以使用NSUserDefaults的傳遞像cookie或會話的Web

+0

我是ios開發新手。可以更具體嗎?是否使用prepareforsegue方法做任何方法 – Nishanth

+0

不要使用NSUserDefaults,這是非常糟糕的做法。 有關您的信息,NSUserDefaults是一個鍵和值的持久性存儲,通常用於像......記住用戶名或一些永久設置。你永遠不會使用它來傳遞數據。 –

+0

@Nishanth不只是練習,如果你不想使用nsuserdefault你可以調用void從第二個控制器傳入的參數傳入第一個控制器 –

0
//The below should not be used in prepare segue.use in any button action. 

if(!secondVC) { 
secondVC = [UIStoryboard storyboardWithName:@"Main" 
           bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SecondVCIdetifier"]; 
} 
secondVC.tempString = tf.text; 
[self presentViewController:secondVC animated:YES completion:nil]; 
+0

- (void)prepareForSegue :(UIStoryboardSegue *)segue sender:(id)sender NextPageController * NXT = segue.destinationViewController; NSLog(@「%@」,_ textfiled); nxt.sr = _textfiled.text; } – Nishanth

+0

我已經在preparefrsegue中編寫了這個,但是它應該在第二個視圖控制器 – Nishanth

+0

的viewdidload中寫什麼,首先你能夠看到tempString中的文本?你想把該字符串放在tableview單元格中?如果是,則在NextViewController的viewdidload方法中寫入[tableView reloadData]; – MuraliMohan

0

[_arr addObject:_sr];第一。查看_sr值是否爲空。如果是的話,你的問題是之前,如果沒有,你的問題是在之後。

從那裏開始,您將能夠查明確切的問題。從快速閱讀我會說你要麼根本沒有得到字符串數據,要麼你沒有使用segue,因此prepareforsegue沒有被調用。

學會使用斷點。如果你不知道如何,查看它,這是非常容易的,這是你工作的70%。

僅憑這個建議,你就能解決你的問題,還有更多的問題來臨。

相關問題