【问题标题】:how to send data from one view controller to another view controller [duplicate]如何将数据从一个视图控制器发送到另一个视图控制器[重复]
【发布时间】:2014-02-23 04:28:13
【问题描述】:

嗨,我是 ios 编程的新手,我正在开发一个日历应用程序,我在其中使用具有 5 个视图控制器的 tabbarcontroller。我的问题是,在第二个视图控制器中,我想更改在第一个视图控制器中显示的日期,我该怎么做。请帮忙....

我在 appdelegate 类中创建了一个属性

@property(nonatomic,assign)NSString *date;

然后在视图控制器和第二个视图控制器中都创建了一个 appdelegate 对象

AppDelegate *obj=[[AppDelegate alloc]init];
    obj.date=@"ABCDSE";

在第一个视图控制器中

 AppDelegate *obj=[[AppDelegate alloc]init];
    mylabel.text=obj.date;

【问题讨论】:

  • 你应该像[[UIApplication sharedApplication]delegate]一样访问appdelegate,同时创建一个新的。
  • 我试过了,但没有发生...
  • 您不需要使用 appdelegate 在视图控制器之间传递数据。使用它们的属性

标签: ios uiviewcontroller appdelegate


【解决方案1】:

您无需创建应用委托,而是获得对它的引用:

AppDelegate *ad = [[UIApplication sharedApplication]delegate];

然后你访问字符串:

mylabel.text = ad.date;

【讨论】:

    【解决方案2】:

    试试这个

    FirstViewController

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
      NSString *strTemp = @"Pass string here";
      FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    
      firstView.Str = strTemp;
    
      [self.navigationController pushViewController:firstView  animated:YES];
    }
    

    SecondViewController.h

    @interface SecondViewController: UIViewController
    @property(nonatomic,retain) NSString *Str;
    @end
    

    看看一些视频教程

    http://www.verious.com/tutorial/ios-5-passing-data-between-view-controllers/ http://www.verious.com/tutorial/iphone-development-tutorial-passing-data-to-another-view/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      相关资源
      最近更新 更多