iOS - 导航控制器和自定义视图

Posted

技术标签:

【中文标题】iOS - 导航控制器和自定义视图【英文标题】:iOS - NavController & Custom Views 【发布时间】:2011-08-08 20:49:08 【问题描述】:

崩溃:

-[UIImageView setParentViewController:]:无法识别的选择器发送到实例 0x58701e * 由于未捕获的 execption 'NSInvalidArgumentExecption' 导致应用程序终止,原因:'-[UIImageView setParentViewController:]: unrecognized selector sent to instance 0x58701e0'

当我尝试推送一个没有 IB 的自定义 UIViewController 时会发生这种情况。

当我使用 IB 执行此操作时,它可以工作,但它并不是我所需要的。

为我的委托调用什么

-(void)switchToTrailerOne 

CGsize screenSize = [UIScreen mainScreen].bounds.size;
CGRect screenBounds = CGRectMake(0, 0, ScreenSize.width, screenSize.height);
TrailersViewController *trailersController = [[TrailersViewController alloc] initWithFrame:screenBounds];

[self.navController pushViewController:trailersController animated:NO]; (Crashes on this line)

[trailersController gotoFirstTrailer];

问我任何其他看起来我几乎从不离开比赛超过一个小时的代码。

编辑:文件有点长,有什么你感兴趣的特定部分吗?我将发布一些我认为可能的候选人......

-(void)loadView

    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGRect screenBounds = CGRectMake(0, 0, screenSize.width, screenSize.height);
    myTrailersView = [[UIImageView alloc] initWithFrame:screenBounds];
    myTrailersView.autoresizesSubviews = YES;
    self.view = myTrailersView;


//Initiation Method
- (void)viewDidLoad 
    [super viewDidLoad];

    //self.myTrailersView.frame = self.view.frame;

    //Turn User Interaction ON - UI Image Views are Off By Default
    myTrailersView.userInteractionEnabled = YES;

    //Set Position Counters to starting values
    currentNode              = 0;
    currentPosition          = 0;

    //Allocate the Node Collection
    nodeCollection           = [NodeSet alloc];

    //Copy the Node Collection to the Receiver Array
    nodeArray                = nodeCollection.createNodeList;

    //Done With Node Collection Release it
    [nodeCollection release];

    //
    //Create the button that launches the cutaway view
    //
    UIBarButtonItem *rotationButton = [[UIBarButtonItem alloc] initWithTitle:@"Cutaway View" 
                                                                       style:UIBarButtonItemStylePlain 
                                                                      target:self 
                                                                      action:@selector(gotoRotationView)];
    self.navigationItem.rightBarButtonItem = rotationButton;
    [rotationButton release];

    ////////Setup the Swipe Forward Recognizer////////

    UISwipeGestureRecognizer *recognizerUp;

    recognizerUp             = [[UISwipeGestureRecognizer alloc]
                    initWithTarget:self action:@selector(handleSwipeFrom:)];

    [recognizerUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [myTrailersView addGestureRecognizer:recognizerUp];
    [recognizerUp release];

    ////////Setup the Swipe Backward Recognizer////////

    UISwipeGestureRecognizer *recognizerDown;

    recognizerDown            = [[UISwipeGestureRecognizer alloc] 
                                    initWithTarget:self action:@selector(handleSwipeFrom:)];

    recognizerDown.direction  = UISwipeGestureRecognizerDirectionDown;
    [myTrailersView addGestureRecognizer:recognizerDown];
    [recognizerDown release];

    ////////Setup the Swipe Left Recognizer////////

    UISwipeGestureRecognizer *recognizerLeft;

    recognizerLeft            = [[UISwipeGestureRecognizer alloc] 
                                    initWithTarget:self action:@selector(handleSwipeFrom:)];

    recognizerLeft.direction  = UISwipeGestureRecognizerDirectionLeft;
    [myTrailersView addGestureRecognizer:recognizerLeft];
    [recognizerLeft release];

    ////////Setup the Swipe Right Recognizer////////

    UISwipeGestureRecognizer *recognizerRight;

    recognizerRight           = [[UISwipeGestureRecognizer alloc] 
                                    initWithTarget:self action:@selector(handleSwipeFrom:)];

    recognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
    [myTrailersView addGestureRecognizer:recognizerRight];
    [recognizerRight release];


- (id)initWithFrame:(CGRect)frame 
    self = [super.view initWithFrame:frame];

    return self;

【问题讨论】:

有趣。如果您可以发布您的 TrailersViewController .h 和 .m 文件会有所帮助,因为一切看起来都在这里。 【参考方案1】:

您似乎在尝试推送自定义UIView,而不是自定义UIViewController。检查TrailersViewController 类层次结构。

【讨论】:

@interface TrailersViewController : UIViewController 或者我错过了什么? 看起来是正确的。也许 TrailersViewController 上的 loadView 出了问题? 问题出在您的 initwithframe 中。您将 self 设置为 [super.view initWithFrame:...]。这意味着您通过调用视图的初始化程序来初始化自己,而不是按预期调用超类。 感谢您的回答。不知何故,从 UIView 而不是 UIViewController 创建了一个类......在这个上卡了半个小时。 :)

以上是关于iOS - 导航控制器和自定义视图的主要内容,如果未能解决你的问题,请参考以下文章

带有嵌套视图控制器的 iOS 自定义导航

自定义 UIView XIB 内的 iOS 导航返回按钮(无导航控制器)

Ios 导航 - 自定义后退按钮或从堆栈中删除视图控制器?

更改导航控制器 ios 的转换

UINavigationController 在 iOS7 中使用自定义转换委托隐藏导航栏

如何在 Swift iOS 中自定义导航控制器