【问题标题】:Different views for different buttons iPhoneiPhone 不同按钮的不同视图
【发布时间】:2010-12-05 10:50:07
【问题描述】:

我是 iPhone 应用程序开发的新手。 我想开发一个 iPhone 应用程序,因为当应用程序启动时,会为用户显示两个按钮。 按钮 1 用于用户登录。 按钮 2 用于用户注册。

如何为每个按钮添加视图,如果按下登录按钮,则该视图加载几个文本字段和一个用于登录的按钮,而如果按下注册按钮,则注册视图加载几个文本字段和一个按钮确认注册。

很少有多个视图的教程,但它们在一个视图上只有一个按钮,按下该按钮下一个视图会加载一个按钮以加载下一个视图,依此类推。就我而言,我希望在加载应用程序时在一个视图上有许多按钮(目前至少 2 个),然后根据按下的按钮加载该视图。

任何示例代码或教程链接将不胜感激。

提前致谢。

【问题讨论】:

    标签: iphone cocoa-touch


    【解决方案1】:

    创建一个方法并注册按钮事件到这个方法

    [button1 addTarget:self 
                action:@selector(buttonClicked:)
      forControlEvents:UIControlEventTouchUpInside];  
    
    [button2 addTarget:self 
                action:@selector(buttonClicked:)
      forControlEvents:UIControlEventTouchUpInside];  
    

    例如:

    -(IBAction)buttonClicked : (id)sender
    {
        UIButton * btn = (UIButton *)sender;
        if (btn == button1) {
            LoginViewController * controller = [[LoginViewController alloc] initWithNibName:@ "LoginViewController" bundle:nil];
            [self.navigationController pushViewController : controller animated : YES];
            [controller release];
        } else if (btn == button2) {
            RegisterViewController * controller = [[RegisterViewController alloc] initWithNibName:@ "RegisterViewController" bundle:nil];
            [self.navigationController pushViewController : controller animated : YES];
            [controller release];
        }
    }
    

    【讨论】:

      【解决方案2】:

      您想在界面生成器中创建视图,然后在按钮一上,您将使用类似代码

      ViewControllerSubClass1 *viewController1=[[ViewControllerSubClass1 alloc] initWithNibName:@"nibname1" bundle:nil];
      [self.navigationController pushViewController:viewController1];
      [viewController1 release];
      

      对于您将使用的按钮二

      ViewControllerSubClass2 *viewController2=[[ViewControllerSubClass2 alloc] initWithNibName:@"nibname2" bundle:nil];
      [self.navigationController pushViewController:viewController2];
      [viewController2 release];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-28
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多