【问题标题】:How to access other viewcontrollers variables/properties in xcode如何在 xcode 中访问其他视图控制器变量/属性
【发布时间】:2012-06-12 07:19:25
【问题描述】:

我知道以前有人问过类似的问题,但请多多包涵,因为我是 Objective C 的新手(对 C 有很好的了解)。

我的情况是我有一个带有两个窗口的标签栏控制器。当我在“秒”中按下一个按钮时,我更改了一个变量changeName。我想在我的“第一个”视图控制器中使用changeName 的值,但偶然发现了一些问题:

为了找到另一个视图控制器,我从 SO 中找到了这个(不幸的是我忘记了在哪里):

-(NSString *)newName{

// Create a UIStoryboard object of "MainStoryBoard_iPhone" named storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:(NSString *)@"MainStoryBoard_iPhone" bundle:(NSBundle *)nil];

// Create a UIViewController object of the wanted element from MainStoryBoard_iPhone
UIViewController *secondview = [storyboard instantiateViewControllerWithIdentifier:(NSString *)@"secondBoard"];

return secondview.changeName;
}

我在我的第一个.m。 MainStoryBoard_iPhone 是 .xib/storyboard 文件的名称。

但没有。错误说

Property 'changeName' not found on object of type 'UIViewController *'

在我的 second.h 我有

 @property (readwrite, retain) NSString *changeName;

在 second.m 中

 @synthesize changeName;

感谢所有帮助,但请记住,我只使用了两天的 OOP。 先谢谢大家了

编辑:我应该在这里输入什么?

 - (void)viewDidLoad
{
[super viewDidLoad];
mylabel.text = Name; // or [mylabel.text Name] or something? 
}

【问题讨论】:

    标签: objective-c ios4 xcode4 uiviewcontroller xcode4.2


    【解决方案1】:

    在我的例子中,我想从 FirstViewController 传递一个字符串来在 SecondViewController 中设置一个文本字段,所以:

    1- 在 SecondViewController.h 中

    @property (strong, nonatomic) IBOutlet UITextField *someTextField;// 这是链接到一个 UITextField

    2- 在 FirstViewController.m 中:

    #import "SecondViewController.h"

    3- 我将在 FirstViewController 中按下按钮后执行此操作:

    SecondViewController *SecondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    //设置字段值

        SecondView.someTextField.text = @"HeLLO from First";
    
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: SecondView];                  
    

    //显示广告视图

       [self.navigationController presentViewController:nav animated:YES completion:nil];
    

    4- 就是这样!

    5- 有一种情况,如果我在 FirstViewController 中并想以相同的方式在 SecondViewController 中设置一个字符串属性,则在您按下 SecondViewController 中的某个按钮之前,该字符串不会获得值,它例如,不会像在 viewDidLoad 中那样得到它!不知道为什么。

    【讨论】:

      【解决方案2】:

      您需要将视图控制器转换为您自定义的第二个视图控制器(因为UIViewController 没有属性changeName)。

      请执行以下操作:

      // Create a UIViewController object of the wanted element from MainStoryBoard_iPhone
      SecondViewController *secondview = (SecondViewController *)[storyboard instantiateViewControllerWithIdentifier:(NSString *)@"secondBoard"]; 
      

      我假设 SecondViewController 是您的第二个控制器的名称。

      【讨论】:

      • 这就是我想要的,但我发现如果我使用SecondViewController *secondview 而不是UIViewController * secondview,我不必强制转换它。但是你能向我解释一下我必须在-(void)viewDidLoad 中插入什么来更改我的标签吗? OOP 中的函数/方法真的让我很困惑。
      • [myLabel setText:@"My Text"] 应该可以解决问题。确保 myLabel 不是 nil(因为这仍然可以正常工作,但不会实际设置任何内容)
      • 谢谢,但这不会将 myLabel 设置为方法 Name 返回的文本,对吗?我如何确保 myLabel 不为零?
      • 我试过[mylabel setText:[self Name]];,没有错误,但我的标签仍然没有任何操作:\按照你的方式做确实会产生文本,所以它应该可以工作,不是吗?
      • 我看到我的问题是我在中间有一个 tabbarcontroller,它也必须得到一个指针 *tabBar,但是 UITabBarController 类没有 instantiateViewControllerWithIdentifier 所以我不知道如何成功在这个
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多