【问题标题】:ModalViewController dismiss view with selectorModalViewController 使用选择器关闭视图
【发布时间】:2012-02-18 09:28:54
【问题描述】:

我有一个从我的主视图显示的模态视图控制器,我在视图的右上角添加了一个“完成”按钮(导航控制器)。但是,我无法让选择器调用正确的方法。这是我用来设置模态视图的代码:

    GraphView *graphView = [[[GraphView alloc] initWithNibName:@"GraphView" bundle:nil] autorelease];

//Set values in the graphView view
[graphView setInterest:interestRateSlider.value / 10];
[graphView setMonths: (loanTermSlider.value / 2.0) * 12]; // Years * 12 = months
[graphView setPrincipal:[principal intValue]];

//show the graph view as a modal navigation controller view
UINavigationController *graphNavigationController = [[UINavigationController alloc] initWithRootViewController:graphView];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] 
                               initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
                               target:graphView
                               action:@selector(dismissView:)];
graphView.navigationItem.rightBarButtonItem = doneButton;
[graphNavigationController.navigationItem.rightBarButtonItem setTitle:@"Done"];
[graphView.navigationItem setTitle:@"Graph"];
[self presentModalViewController:graphNavigationController animated:YES];
[graphNavigationController release];
[doneButton release];

然后在我的 graphView 类中我有方法:

 -(void) dismissView {
     [self dismissModalViewControllerAnimated: YES];
 }

但是,在运行代码时,我得到了无法识别的选择器。选择器正在尝试调用 UINavigationController 变量上的方法。如何让选择器调用正确的方法?

谢谢

【问题讨论】:

    标签: iphone objective-c uinavigationcontroller modalviewcontroller


    【解决方案1】:

    你在调用选择器时弄错了:

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] 
                               initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
                               target:graphView
                               action:@selector(dismissView:)];
    

    当dismissView被定义为

    -(void) dismissView;
    

    你应该将dismissView定义为

    -(void) dismissView:(id)sender;
    

    或者不带冒号的称呼

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] 
                               initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
                               target:graphView
                               action:@selector(dismissView)];
    

    【讨论】:

    • 是的,这就是它只需要在没有冒号的情况下调用。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多