应用程序窗口应该在应用程序结束时具有根视图控制器

时间:2014-01-29 11:26:09

标签: ios iphone objective-c uikit

我有一个问题,我已经阅读了以下解决方案。这是我的代码

AppDelegat.h

#import <UIKit/UIKit.h>
@class ViewController;


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property(strong,nonatomic)ViewController *vobj;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "ViewController.h"


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window= [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.vobj = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.vobj;

    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

并且它给出了错误应用程序窗口应该在应用程序启动结束时具有根视图控制器

这是我的代码

https://www.dropbox.com/s/y3gzur3tb032nz3/slide.zip

Applications are expected to have a root view controller at the end of application launch
Application windows are expected to have a root view controller at the end of application launch warning
Application windows are expected to have a root view controller at the end of application launch - even with all known issues fixed
和其他链接...

谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了问题的根源,并使其成功。它比看起来更深。一般的问题是,在“合成”线上,您还设置了要合成的“视图”出口。 “视图”在视图创建时自动与iOS中的视图控制器中的“视图”属性相关联,您只是对该关联进行了覆盖。

将您的行更改为此

@synthesize slide1,slide2,slide3,segmentview,segment1,segment2,switch1,newslider,lbl;

你会没事的。问题是虽然您的视图控制器已实例化并指定为根视图控制器,但其“view”属性设置为nil。你可以看到使用调试器。 iOS可能将nil视图解释为未分配的根视图控制器,因此您看到了错误。

另外,我可以就如何节省时间,冗余并使您的代码更清洁给您一些建议吗?请考虑以下代码:

@interface ViewController : UIViewController
{

//    UISlider *slide1;
//    UISlider *slide2;
//    UISlider *slide3;
//    UIView   *newslider;
//    UIView  *segment1;
//    UIView  *segment2;
//    UISegmentedControl *segmentview;
//    UILabel *lbl;
//    UISwitch *switch1;
//    



}
@property (strong,nonatomic)IBOutlet UISlider *slide1;
@property (strong,nonatomic)IBOutlet UISlider *slide2;
@property (strong,nonatomic)IBOutlet UISlider *slide3;
@property (strong,nonatomic)IBOutlet UIView   *newslider;
@property (strong,nonatomic)IBOutlet UIView  *segment1;
@property (strong,nonatomic)IBOutlet UIView *segment2;
@property (strong,nonatomic)IBOutlet UISegmentedControl *segmentview;
@property (strong,nonatomic)IBOutlet UILabel *lbl;
@property (strong,nonatomic)IBOutlet UISwitch *switch1;


-(IBAction)btnchangecolor:(id)sender;

@end

然后,您的视图控制器实现将如下所示:

@implementation ViewController
//@synthesize slide1,slide2,slide3,segmentview,segment1,segment2,switch1,view,newslider,lbl;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)loadView {
    [super loadView];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
-(IBAction)btnchangecolor:(id)sender
{
    if (_segmentview.selectedSegmentIndex==0) {
        _segment1.backgroundColor=[UIColor colorWithRed:_slide1.value/255 green:_slide2.value/255 blue:_slide3.value/255 alpha:1.0];
    }
    else
    {
        _segment2.backgroundColor=[UIColor colorWithRed:_slide1.value/255 green:_slide2.value/255 blue:_slide3.value/255 alpha:1.0];
    }


    _lbl.backgroundColor=[UIColor colorWithRed:_slide1.value/255 green:_slide2.value/255 blue:_slide3.value/255 alpha:1.0];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

当您创建@property时,现代Xcode版本会生成具有该名称的@property,并将“myVar”等属性的内部变量自动设置为“_myVar”。在自动合成功能之前,您可以通过编写“@synthesize myVar = _myInternalVar”来设置内部变量的名称,这将允许您编写“_myInternalVar = newValue”或“self.myVar = newValue”(您仍然可以这样做,顺便说一句,如果你愿意)。然而,看到你的代码,似乎有一些冗余。您创建iVars,然后创建IBOutlets,然后使用iVars的名称合成出口。这不是一个错误,它甚至不是一个警告,但你可以通过只写“@property”元素来节省这么多时间,而不是其他任何东西,我可能只是把它当作一个选项:)