【问题标题】:Passing data between two view controllers在两个视图控制器之间传递数据
【发布时间】:2012-07-27 01:38:24
【问题描述】:

我有两个视图控制器,分别称为 DataPass 和 ViewController。我想将数据从 DataPass 传递到 ViewController。我在 ViewController 中有一个标签,在我的 ViewController 中有一个 UIButton,它会自行关闭,并且在关闭时会将数据传递给 DataPass 中的标签。

我做不到。请帮忙。这是我的代码:

ViewController.h

@interface ViewController : UIViewController
- (IBAction)sayfaGec:(id)sender;
@property (retain, nonatomic) IBOutlet UILabel *label;
+(ViewController *)hop;

ViewController.m

+(ViewController *)hop{
    static ViewController *myInstance = nil;
    if (myInstance == nil){
        myInstance = [[[self class]alloc]init];
        myInstance.label.text = @"test";
    }
    return myInstance;
}

DataPass.h

- (IBAction)sayfaKapat:(id)sender;

DataPass.m

- (IBAction)sayfaKapat:(id)sender {
    [ViewController hop].label.text = @"ddsg";
    [self dismissModalViewControllerAnimated:YES];
}

【问题讨论】:

  • 有什么问题——没有更多的代码很难说为什么这不起作用。解雇是否有效,只是将文本分配给标签失败?可能标签在 IB 中没有正确连接?
  • @ErikH。是的,问题只是将文本分配给标签失败。我是IOS开发的新手。我从不在控制器之间传递数据。但是标签、按钮和视图控制器之间的所有连接都是正确的。

标签: ios objective-c iphone cocoa-touch uiviewcontroller


【解决方案1】:
ViewController.h

- (void)setLabelData:(NSString:)aString;

ViewController.m

- (void)setLabelData:(NSString:)aString{
    [yourLabel setText:aString];
}

在你的 DataPass 中你说:

ViewController *vContr = [[ViewController aloc]init];
[vContr setLabelData: @"asta la vista baby!"];
[self.view addSubview:vContr.view];

【讨论】:

  • 它应该可以工作:创建 ViewController 对象后,您可以访问它声明的所有实例方法。检查标签上的插座,它是否正确挂在 xib 中?。
  • 我正在使用情节提要。不是xib文件。一个
  • 在您的故事板中,您应该会看到 viewController 的控制器。您必须将插座连接到您的标签上,否则将无法正常工作。
  • 顺便问一下,您是在以编程方式创建标签吗?
  • 我是用鼠标左键拖入.h文件中创建的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多