ModalViewController 使用选择器关闭视图

发布于 2024-12-29 05:31:47 字数 1424 浏览 2 评论 0原文

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

    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 变量上的方法。如何让选择器调用正确的方法?

谢谢

I have a modal view controller that is presented from my main view, I am adding a "Done" button in the top right hand side of the view (navigationcontroller). However, I can't get the selector to call the right method. Here is the code I am using to set up the modal view:

    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];

Then in my graphView class I have the method:

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

However, when running the code I get unrecognized selector. The selector is trying to call the method on the UINavigationController variable. How can I get the selector to call the right method?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2025-01-05 05:31:47

调用选择器时犯了一个错误:

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

当您定义missView时,

-(void) dismissView;

您应该将dismissView定义为

-(void) dismissView:(id)sender;

或不带冒号调用它

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

You made a mistaken when calling the selector:

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

when dismissView is defined as

-(void) dismissView;

you should define dismissView as

-(void) dismissView:(id)sender;

or call it without colon

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] 
                           initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
                           target:graphView
                           action:@selector(dismissView)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文