在视图之间转换时的 Objective-C 问题

发布于 2024-12-28 07:30:30 字数 2585 浏览 4 评论 0原文

我在视图之间转换时遇到问题,需要一些帮助。这有点令人费解,所以请耐心等待。

我有一个名为 JobsNavController 的 UINavigationController。 JobsNavController 中的第一个视图包含一个名为 JobsTableViewController 的 UITableViewController [带有名为 JobTableView.xib 的链接笔尖]。我想在 UINavController 内添加一个 Add UIButton 来“创建新作业”。单击时,它应该从 JobTableView.xib 翻转到我的名为 JobCreateView.xibJobCreateViewController 笔尖。由于“添加”按钮位于 UINavController 内,因此我将 IBAction 代码放入 JobsNavController.h 和 .m 内。

这是 JobsNavController.h

#import <UIKit/UIKit.h>
@class JobCreateViewController, JobsTableViewController;

@interface JobsNavController : UINavigationController {
    IBOutlet UIButton *btnJobCreate;
    IBOutlet JobCreateViewController *jobCreateViewController;
        IBOutlet JobsTableViewController *jobsTableViewController;
}
-(IBAction)tellDelegateToFlip:(id)sender;

@property (nonatomic, retain) UIButton *btnJobCreate;
@property (nonatomic, retain) IBOutlet JobCreateViewController *jobCreateViewController;
@property (nonatomic, retain) IBOutlet JobsTableViewController *jobsTableViewController;

@end

这是我的 JobsNavController.m

#import "JobsNavController.h", "Time_Blogger1AppDelegate.h", "JobsTableViewController.h"
@implementation JobsNavController
@synthesize btnJobCreate, jobCreateViewController, jobsTableViewController;
.....
-(void)tellDelegateToFlip {
    JobCreateViewController *jobAddView = [jobCreateViewController initWithNibName:@"JobCreateView" bundle:nil];

    [self setJobCreateViewController:jobAddView];
    [jobAddView release];

    UIViewController *transitionTo = jobCreateViewController;

    //create view animation block
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    [jobsTableViewController.view removeFromSuperview];
    [self.view insertSubview:transitionTo.view atIndex:0];
    [UIView commitAnimations];

    [transitionTo release];
}

我没有收到任何构建/编译错误,但当我单击按钮时模拟器会抛出异常,说明:

2012-01-22 19:19:22.895 Time-Blogger1[4209:f803] 
-[JobsNavController tellDelegateToFlip:]: unrecognized selector sent to instance 0x6c85e80 2012-01-22 19:19:22.897 Time-Blogger1[4209:f803] 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[JobsNavController tellDelegateToFlip:]: unrecognized selector sent to instance 0x6c85e80'

I'm having issues transitioning between views and need some help. This is somewhat conviluted so bear with me please.

I have a UINavigationController called JobsNavController. The first view in JobsNavController holds a UITableViewController called JobsTableViewController [with a linked nib called JobTableView.xib]. I want to add an Add UIButton inside the UINavController to 'create a new job'. When clicked, it should flip from JobTableView.xib to my JobCreateViewController nib called JobCreateView.xib. Since the 'add' button is located within the UINavController I put the IBAction code inside JobsNavController.h and .m.

Here is the JobsNavController.h

#import <UIKit/UIKit.h>
@class JobCreateViewController, JobsTableViewController;

@interface JobsNavController : UINavigationController {
    IBOutlet UIButton *btnJobCreate;
    IBOutlet JobCreateViewController *jobCreateViewController;
        IBOutlet JobsTableViewController *jobsTableViewController;
}
-(IBAction)tellDelegateToFlip:(id)sender;

@property (nonatomic, retain) UIButton *btnJobCreate;
@property (nonatomic, retain) IBOutlet JobCreateViewController *jobCreateViewController;
@property (nonatomic, retain) IBOutlet JobsTableViewController *jobsTableViewController;

@end

And here is my JobsNavController.m

#import "JobsNavController.h", "Time_Blogger1AppDelegate.h", "JobsTableViewController.h"
@implementation JobsNavController
@synthesize btnJobCreate, jobCreateViewController, jobsTableViewController;
.....
-(void)tellDelegateToFlip {
    JobCreateViewController *jobAddView = [jobCreateViewController initWithNibName:@"JobCreateView" bundle:nil];

    [self setJobCreateViewController:jobAddView];
    [jobAddView release];

    UIViewController *transitionTo = jobCreateViewController;

    //create view animation block
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    [jobsTableViewController.view removeFromSuperview];
    [self.view insertSubview:transitionTo.view atIndex:0];
    [UIView commitAnimations];

    [transitionTo release];
}

I'm not getting any build/compile errors but the simulator throws an exception when I click the button stating:

2012-01-22 19:19:22.895 Time-Blogger1[4209:f803] 
-[JobsNavController tellDelegateToFlip:]: unrecognized selector sent to instance 0x6c85e80 2012-01-22 19:19:22.897 Time-Blogger1[4209:f803] 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[JobsNavController tellDelegateToFlip:]: unrecognized selector sent to instance 0x6c85e80'

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

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

发布评论

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

评论(2

梦行七里 2025-01-04 07:30:30

当您调用 tellDelegateToFlip 时,请确保您没有使用任何参数 - 这就是导致崩溃的原因。如果您在崩溃报告中注意到,它表示无法识别的选择器 tellDelegateToFlip: 正在发送到您的实例。请注意方法名称后面的冒号。这意味着无论您在哪里调用该方法,都会随之发送一个对象。如果您使用performSelector:withObject:afterDelay:,请确保您使用冒号。

编辑:

而不是:

UIViewController* transitionTo = jobCreateViewController;

为什么不直接使用:

JobCreateViewController* transitionTo = jobCreateViewController;

或者您可以强制转换它,假设 JobCreateViewController 继承自 UIViewController。

When you call tellDelegateToFlip make sure you do it with no parameters- this is what is causing the crash. If you notice in the crash report, it says the unrecognized selector tellDelegateToFlip: is being sent to your instance. Notice the colon coming after the method name. This means that wherever you called the method, you sent an object with it. If you are using performSelector:withObject:afterDelay: make sure you don't use a colon.

Edit:

Instead of:

UIViewController* transitionTo = jobCreateViewController;

why don't you just use:

JobCreateViewController* transitionTo = jobCreateViewController;

Or you could cast it, assuming that JobCreateViewController inherits from UIViewController.

dawn曙光 2025-01-04 07:30:30

该方法的错误实现:

-(IBAction)tellDelegateToFlip:(id)sender;

应该是:

-(IBAction)tellDelegateToFlip:(id)sender {
...
}

在您的 JobsNavController.m 中

wrong implementation of the method:

-(IBAction)tellDelegateToFlip:(id)sender;

should be:

-(IBAction)tellDelegateToFlip:(id)sender {
...
}

in your JobsNavController.m

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文