处理意见和其他一些事情

时间:2012-01-29 18:03:31

标签: iphone objective-c

我正在学习iOS开发。现在正在玩意见。我目前不使用界面构建器,因为我更喜欢了解所有东西是如何工作的。这就是我以编程方式创建视图UI元素等的原因。然而。 这是我的项目的样子。

我有一个名为RootViewController的类。我使用该类作为窗口rootViewController。 在该类中,我有一个加载/卸载视图的逻辑。这是一些代码。

RootViewController.m

-(void)loadView
{
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    self.view = view;
    self.view.backgroundColor = [UIColor lightGrayColor];

    // create FirstViewController button
    UIButton *firstViewButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
             firstViewButton.frame = CGRectMake(20, 60, 280, 50);
             firstViewButton.tag = 1;
             [firstViewButton setTitle:@"Load First view" forState:UIControlStateNormal];
             [firstViewButton addTarget:self 
                   action:@selector(loadViewControlllers:) 
             forControlEvents:UIControlEventTouchUpInside];

    // crate SecondViewController button
    UIButton *secondViewButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
             secondViewButton.frame = CGRectMake(20, 140, 280, 50);
             secondViewButton.tag = 2;
             [secondViewButton setTitle:@"Load Second view" forState:UIControlStateNormal];
             [secondViewButton addTarget:self 
                        action:@selector(loadViewControlllers:) 
              forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:firstViewButton];
    [self.view addSubview:secondViewButton];
    [self.view release];
}

-(void)loadViewControlllers:(UIButton *)sender
{
    if ([sender tag] == 1) {
        if(firstViewController == nil) {
            firstViewController = [[FirstViewController alloc]
                                   initWithNibName:@"FirstViewController" 
                                   bundle:nil];
            [self.view addSubview:firstViewController.view];
        }  
    } else {
        if(secondViewController == nil) {
            secondViewController = [[SecondViewController alloc]
                                    initWithNibName:@"SecondViewController" 
                                   bundle:nil];
            [self.view addSubview:secondViewController.view];
        }
    }
}

第一个和第二个视图控制器包含相同的代码

-(void)loadView
{
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];   
     self.view = view;

    self.view.backgroundColor = [UIColor redColor];

    UIButton *remove = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    remove.frame = CGRectMake(20, 60, 280, 50);
    [remove setTitle:@"Remove First view from superview" forState:UIControlStateNormal];
    [remove addTarget:self 
               action:@selector(removeFromView:) 
     forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:remove];
    [self.view release];
}

-(IBAction)removeFromView:(id)sender
{
    [self.view removeFromSuperview];
}

到目前为止,这么好。 这是我的问题开始的地方。

1)这是显示/删除视图的正确方法吗?一个控制器(在我的情况下是rootViewController)来统治它们吗?

2)你能建议我在第一,第二和根视图之间传递数据的方法吗?让我们说第一个ViewController被加载了。该控制器包含文本字段和按钮。我想要的是当用户在文本字段中键入内容并按下按钮时,该文本字段中的数据能够通过rootViewController和secondViewController读取

3)应用程序运行良好。没有崩溃等问题是,当第一个或第二个控制器都没有被调用时,一旦它们无法再次加载。 当我按下按钮时,没有任何事情发生。

最后但不要忘记,抱歉我的语言。

1 个答案:

答案 0 :(得分:0)

  

1)这是显示/删除视图的正确方法吗?一个控制器   (在我的情况下是rootViewController)来统治它们吗?

没关系。但是,如果firstViewController和secondViewController共享完全相同的逻辑,那么它们实际上是一个类,并且通常可以安全地删除冗余。

  

2)你能否建议我在第一,第二和第二之间传递数据的方法   根视图?让我们说第一个ViewController被加载了。那个控制器   包含文本字段和按钮。我想要的是当用户输入时   文本字段中的某些内容和按钮按下了该数据   textfield能够通过rootViewController读取和   secondViewController

您可以使用单例实例来保存共享状态(MVC模式中的模型),但不建议这样做。 恕我直言,您可以使用具有键值观察模式的观察者模型。请参阅Apple的指南here。或者,您可以从一个控制器发布NSNotification,以通知对该类型通知感兴趣的任何其他控制器。请参阅[此处的指南]。2

  

3)应用程序运行良好。没有崩溃等问题是   当第一个或第二个控制器都不能被调用时   再次加载。当我按下按钮时,没有任何事情发生。

在上面的代码中,只有当控制器ivars为nil时才会添加子视图。尝试修复这个逻辑。

相关问题