0

首先,我是 iOS 和 Objective-C 编程的新手(我是一个月前开始的)。

我正在尝试做一个自定义的tabBar,它在我的带有iOS 5.1.1 的iPhone 4 中运行良好,但它不适用于带有iOS 4.2.1 的3GS。我会更新它以查看它是否与 iOS 版本而不是手机型号相关,但我更喜欢在 iOS 4 中进行测试。

  • 首先,如果有人帮助我找出导致此崩溃的原因以及如何避免它,我将非常高兴。好吧,正如调用堆栈中显示的那样,我的 viewController 似乎没有响应transitionFromViewController:toViewController:duration:options:animations:completion:,但它在 iOS 5 中工作......难道它是 iOS 4 中不存在的新功能?如果这是刚刚发生的事情,我如何在没有这个选择器的情况下从一个 viewController 转换到另一个?

  • 其次,我想知道我的做法是否正确:正如我之前提到的,我想要的是我自己的自定义 TabBar。我所做的是制作一个具有 5 个按钮的 viewController,并且几乎整个屏幕都是一个带有插座的视图,可以根据按下的按钮在其中加载相应的 viewController。例如:

- 标题:

//  CustomTabBarViewController.h
#import <UIKit/UIKit.h>
#import "Tab1ViewController.h"

@interface TabBarCustomViewController : UIViewController

// View Controllers
@property (retain, nonatomic) Tab1ViewController *myTab1ViewController;
@property (retain, nonatomic) Tab2ViewController *myTab2ViewController;

// Reference of the current shown viewController, for the transitions
@property (retain, nonatomic) UIViewController *currentViewController; 

// Outlet to switch the viewController inside the view mainView;
@property (retain, nonatomic) IBOutlet UIView *mainView;

- .m 文件

#import "TabBarCustomViewController.h"

@interface TabBarCustomViewController ()

@end

@implementation TabBarCustomViewController

@synthesize myTab1ViewController, myTab2ViewController;
@synthesize currentViewController;
@synthesize mainView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    //Instantiation of the viewControllers
    self.myTab1ViewController = [[Tab1ViewController alloc]initWithNibName:@"Tab1ViewController" bundle:nil];
    self.myTab2ViewController = [[Tab2ViewController alloc]initWithNibName:@"Tab2ViewController" bundle:nil];


    // Adding those viewControllers to the root viewController  
    [self addChildViewController:self.myTab1ViewController];
    [self addChildViewController:self.myTab2ViewController];


    //the entry view
    [mainView addSubview:self.myTab1ViewController.view];
    currentViewController = self.myTab1ViewController;   
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


// My methods

- (IBAction)tab1Pressed
{
    NSLog(@"tab1Pressed");

    [self transitionFromViewController:currentViewController toViewController:self.myTab1ViewController duration:0.2    options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:nil];

    // Updating the reference of which viewController is currently being displayed
    currentViewController = myTab1ViewController;
}

- (IBAction)tab2Pressed
{
    NSLog(@"tab2Pressed");

    [self transitionFromViewController:currentViewController toViewController:self.myTab2ViewController duration:0.2    options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:nil];

    // Updating the reference of which viewController is currently being displayed
    currentViewController = myTab2ViewController;
}

@end

- 这是发生第一点提到的崩溃时控制台中抛出的内容:

2012-06-08 08:25:55.991 myTabBar[4597:307] tab2Pressed
2012-06-08 08:25:56.003 myTabBar[4597:307] -[TabBarCustomViewController transitionFromViewController:toViewController:duration:options:animations:completion:]: unrecognized selector sent to instance 0x143cc0
2012-06-08 08:25:56.028 myTabBar[4597:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TabBarCustomViewController transitionFromViewController:toViewController:duration:options:animations:completion:]: unrecognized selector sent to instance 0x143cc0'
*** Call stack at first throw:
(
0   CoreFoundation                      0x314d0987 __exceptionPreprocess + 114
1   libobjc.A.dylib                     0x319a149d objc_exception_throw + 24
2   CoreFoundation                      0x314d2133 -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
3   CoreFoundation                      0x31479aa9 ___forwarding___ + 508
4   CoreFoundation                      0x31479860 _CF_forwarding_prep_0 + 48
5   myTabBar                            0x00003707 -[TabBarCustomViewController tab2Pressed] + 126
6   CoreFoundation                      0x31477fed -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
7   UIKit                               0x338c14ad -[UIApplication sendAction:to:from:forEvent:] + 84
8   UIKit                               0x338c144d -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
9   UIKit                               0x338c141f -[UIControl sendAction:to:forEvent:] + 38
10  UIKit                               0x338c1171 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
11  UIKit                               0x338c19cf -[UIControl touchesEnded:withEvent:] + 342
12  UIKit                               0x338b7355 -[UIWindow _sendTouchesForEvent:] + 368
13  UIKit                               0x338b6ccf -[UIWindow sendEvent:] + 262
14  UIKit                               0x338a1fc7 -[UIApplication sendEvent:] + 298
15  UIKit                               0x338a1907 _UIApplicationHandleEvent + 5090
16  GraphicsServices                    0x35d66f03 PurpleEventCallback + 666
17  CoreFoundation                      0x314656ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
18  CoreFoundation                      0x314656c3 __CFRunLoopDoSource1 + 166
19  CoreFoundation                      0x31457f7d __CFRunLoopRun + 520
20  CoreFoundation                      0x31457c87 CFRunLoopRunSpecific + 230
21  CoreFoundation                      0x31457b8f CFRunLoopRunInMode + 58
22  GraphicsServices                    0x35d664ab GSEventRunModal + 114
23  GraphicsServices                    0x35d66557 GSEventRun + 62
24  UIKit                               0x338d5329 -[UIApplication _run] + 412
25  UIKit                               0x338d2e93 UIApplicationMain + 670
26  myTabBar                            0x000027cb main + 102
27  myTabBar                            0x00002760 start + 40
)
terminate called after throwing an instance of 'NSException'
(lldb) 

我知道我可以使用[mainView addSubview:myTab1ViewController.view]; 而不是[self transitionFromViewController:currentViewController toViewController:self.myTab2ViewController duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:nil];

但是这种方式我不能做过渡动画,除了我不确定这是否是做我想做的事情的正确方法。

先感谢您!

4

1 回答 1

2

根据官方文档: http: //developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/transitionFromViewController :toViewController:duration:options :动画:完成

transitionFromViewController:toViewController:duration:options:animations:completion 可用:

可用性
适用于 iOS 5.0 及更高版本。

所以你可以期待unrecognized selector sent to instanceiOS 5.0 之前的所有版本

对于自定义 TabBar,您可能需要查看以下帖子:http:
//mobiledevelopertips.com/open-source/ios-open-source-custom-tabbar-controller-bctabbarcontroller.html
https://stackoverflow.com/a/4912328/ 200272

于 2012-06-08T08:09:04.257 回答