【问题标题】:Objective-C Problems While Transitioning Between Views在视图之间转换时的 Objective-C 问题
【发布时间】:2012-01-23 01:36:36
【问题描述】:

我在视图之间转换时遇到问题,需要一些帮助。这有点令人困惑,所以请多多包涵。

我有一个名为 JobsNavController 的 UINavigationController。 JobsNavController 中的第一个视图包含一个名为 JobsTableViewController 的 UITableViewController [带有一个名为 JobTableView.xib 的链接 nib]。我想在 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'

【问题讨论】:

  • 如果您在调用 tellDelegateToFlip 的位置添加代码,我想我可以准确地告诉您崩溃的位置/原因。
  • @Rickay 这是我为 tellDelegateToFlip 提供的所有代码
  • 我的意思是你调用方法的代码,而不是实现它。
  • 我在上面的 JobsNavController.h 代码部分中显示了对方法的调用

标签: objective-c xcode uiviewcontroller transition nsexception


【解决方案1】:

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

编辑:

代替:

UIViewController* transitionTo = jobCreateViewController;

你为什么不直接使用:

JobCreateViewController* transitionTo = jobCreateViewController;

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

【讨论】:

  • 这解决了我的问题,但是我现在在UIViewController *transitionTo = ... 线上收到警告。陈述:“不兼容的指针类型初始化 'UIViewController *' 与类型的表达式 'JobCreateViewController *'”
  • 我不清楚你为什么用 UIViewController 类型声明它。为什么不直接将其声明为JobCreateViewController*
  • 你能提供我会使用的语法行替换吗?
  • 当我实施您的建议时,我在这一行收到以下错误:[self.view insertSubview:transitionTo.view atIndex:0];, Property 'view' cannot be found in forward class object 'JobCreateViewController *'
  • JobCreateViewController 属于哪个类?
【解决方案2】:

方法的错误实现:

-(IBAction)tellDelegateToFlip:(id)sender;

应该是:

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

在您的 JobsNavController.m 中

【讨论】:

    猜你喜欢
    • 2013-05-08
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    相关资源
    最近更新 更多