在视图间切换时出现Objective-C问题

问题描述:

我在视图之间转换问题并需要一些帮助。这有点令人费解,请耐心等待。在视图间切换时出现Objective-C问题

我有一个名为JobsNavController的UINavigationController。 JobsNavController中的第一个视图包含一个名为JobsTableViewController的UITableViewController [带有一个名为JobTableView.xib的链接的笔尖]。我想在UINavController中添加一个Add UIButton来“创建一个新的工作”。点击后,它应该从JobTableView.xib翻转到我的JobCreateViewController笔尖JobCreateView.xib。由于“添加”按钮位于UINavController内,因此我将IBAction代码放入JobsNavController.h and .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' 
+0

如果添加在您调用'tellDelegateToFlip'的代码,我想我可以告诉你究竟在哪里/为什么它崩溃。 –

+0

@Rickay这就是我所有的'tellDelegateToFlip'代码 – sadmicrowave

+0

我的意思是你调用方法的代码,而不是实现它的方法。 –

当您拨打电话tellDelegateToFlip确保你没有参数,这是导致崩溃的原因。如果您在崩溃报告中注意到,它表示无法识别的选择器​​正在发送到您的实例。注意方法名后面的冒号。这意味着无论你调用方法,你都会发送一个对象。如果您使用的是performSelector:withObject:afterDelay:,请确保您的不要使用冒号

编辑:

相反的:

UIViewController* transitionTo = jobCreateViewController; 

你为什么不只是使用:

JobCreateViewController* transitionTo = jobCreateViewController; 

或者你可以施放它,假定从UIViewController中JobCreateViewController继承。

+0

上面我的JobsNavController.h代码部分显示了对该方法的调用,它解决了我的问题,但是现在我正在'UIViewController * transitionTo = ...'行发出警告。说:“不兼容的指针类型初始化'UIViewController *'与类型的表达式'JobCreateViewController *'” – sadmicrowave

+0

我不明白为什么你声明它与类型UIViewController。为什么不把它声明为'JobCreateViewController *'? –

+0

你可以提供语法行替换我会用你的方式吗? – sadmicrowave

错误的方法实现:

-(IBAction)tellDelegateToFlip:(id)sender; 

应该是:

-(IBAction)tellDelegateToFlip:(id)sender { 
... 
} 
在JobsNavController.m