2014-10-10 36 views
-1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
SecondViewController *second=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 
second.getString=[getArray objectAtIndex:indexPath.row]; 
[self performSegueWithIdentifier:@"Action" sender:nil]; } 

如何從一個視圖數據傳遞到另一個,在我的應用程序有一些對象,我想,當我在任何電池接頭則該值應在其中具有UILabel下一個視圖控制器顯示?怎麼可能通過didSelectRowAtIndexPath將數據從tableview控制器傳遞到下一個viewController(即不使用Segue)?

+0

如果你想瀏覽下一個'viewController'那麼你必須執行,否則賽格瑞你可以替換賽格瑞'[self.navigationController pushViewController:第二動畫:是] ;'導航並傳遞數據。 – 2014-10-10 05:22:35

+2

關於將數據從一個控制器傳遞到另一個控制器的問題確實應該從SO中被禁止。他們已經被問及已經回答了幾千次了。 – rdelmar 2014-10-10 05:29:53

+0

Segue標識符是否也會聲明? – 2014-10-10 05:32:58

回答

1

在您的SecondViewController中創建一個init方法,如下所示。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withData:(NSString *) data { 
    if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     self.data = data; 
     // write your own code here 
    } 
    return self; 
} 

在你的tableview的didSelectRowAtIndexPath排方法得到字符串,然後按下控制器如下圖所示

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSSting *stringData = @"hi";// get your data and passing to stringData 
SecondViewController *secondVC=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil withData:stringData]; 
    [self.navigationController pushViewController: secondVC animated:YES]; 
} 
0

試試這個

創建變量的實例NSString * sendingText ;// if you want to send text(string)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    sendingText = [getArray objectAtIndex:indexPath.row]; 
    [self performSegueWithIdentifier:@"Action" sender:nil]; 
} 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"Action"]) // this check is optional if there is only on segue 
    {  
     SecondViewController *second= (SecondViewController *)segue.destinationViewController; 
     second.getString = sendingText; 
    } 
} 

SecondViewController.h

@interface SecondViewController : UIViewController 
{ 
    UILabel * exampleLabel; 
} 
@property NSString * getString; 
@end 

SecondViewController.m

-(void)viewDidLoad 
{ 
    exampleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 100, 30)]; 
    [self.view addSubview:exampleLabel]; 
} 
-(void)viewWillAppear:(BOOL)animated 
{ 
    exampleLabel.text = getString; 
} 
+0

通過此代碼我們瀏覽嵌套視圖和onconsole它顯示>>>嵌套的推動畫可能導致損壞的導航欄 – 2014-10-10 06:42:45

+0

意外觸發相同的分段TWICE一次在代碼中,一次從界面構建器,但在同一時間...所以直接從單元中刪除segue並將其從視圖控制器連接起來 – raki 2014-10-10 07:00:22

相關問題