【问题标题】:Correctly link my buttons to my views (in UINavigationController)?正确地将我的按钮链接到我的视图(在 UINavigationController 中)?
【发布时间】:2012-05-24 23:07:10
【问题描述】:

如果这个问题听起来有点新手,我很抱歉,但我是初学者,想改进。所以,我有一个嵌入我的整个应用程序的导航控制器。我的主视图有一个按钮,链接到另一个视图。

这是我在主视图中所做的:

.h

@property (nonatomic, retain) IBOutlet UIButton *button;

在 .m 中,我的按钮的一些代码。

在 IB 中,我在主视图中添加了一个按钮,并在另一个 ViewController 中添加了我更改了类的视图。以及以下链接:

  • 按钮 -> 按钮
  • 按钮 -> OtherViewController (Push)

现在我的问题是,我想在我的代码或 IB 中添加什么?我也需要 IBAction 吗?

非常感谢您的建议..

【问题讨论】:

    标签: objective-c ios uinavigationcontroller interface-builder uibutton


    【解决方案1】:
    -(IBAction)clickButton:(id)sender{
    
    if (!createViewController) {
                    createViewController = [[CreateViewController alloc] initWithNibName:@"CreateViewController" bundle:nil];
    
                }
    
                UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
                self.navigationItem.backBarButtonItem = backBarButtonItem;
                [backBarButtonItem release];
                [self.navigationController pushViewController:createViewController animated:YES];
    }
    

    【讨论】:

      【解决方案2】:

      是的,您需要一个定义按钮操作的方法。

      在.h中:

      - (IBAction)buttonPressed:(id)sender;
      

      在.m:

      - (IBAction)buttonPressed:(id)sender { // what you want to do when the button is pressed }
      

      然后在您的界面中单击按钮,从 Connection Inspector 中,控制单击“Touch Up Inside”中的单选按钮,连接到 File's Owner 并选择方法 -buttonPressed。

      【讨论】:

      • 非常感谢您的回答。我有 2 个问题: - 我需要删除 IB 链接按钮 -> OtherController (Push) 吗? - 如何通过代码显示我的其他视图?再次非常感谢。
      • 在你的方法 buttonPressed 中,你需要: YourViewController *resultView = [[YourViewController alloc]initWithNibName:@"YourViewController" bundle:nil]; [self presentModalViewController:resultView Animation:NO]; [结果查看发布];
      猜你喜欢
      • 2018-02-17
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 2016-04-10
      • 2017-07-04
      • 1970-01-01
      相关资源
      最近更新 更多