0

I have one problem and I've read following solution.This is my code

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;
}

and it gives an error Application windows are expected to have a root view controller at the end of application launch

This is my code

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
and other link...

Thank You.

Community
  • 1
  • 1
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45

1 Answers1

0

I found the source of the problem, and I made it work. It is deeper than it seems. The general problem is that on your "synthesised" line, you have also set the 'view' outlet to be synthesised. The 'view' is automatically associated on view creation with the 'view' property inside a view controller by iOS, and you just did an override on that association.

Change your line to this

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

and you will be fine. The problem was that although your view controller was instantiated and assigned as a root view controller, its 'view' property was set to nil. You could see that using the debugger. iOS probably interpreted the nil view as an unassigned root view controller, hence the error you were seeing.

On a side note, might I give you some advice on how to save time, redundancy and make your code cleaner? Consider the following code:

@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

Then, your view controller implementation would be like this:

@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

When you create a @property, modern Xcode versions produce a @property with that name, and sets the internal variable of a property like "myVar" automatically to "_myVar". Before automatic synthesizing feature, you could set the name of the internal variable by writing "@synthesize myVar = _myInternalVar", which would allow you to write "_myInternalVar = newValue" or "self.myVar = newValue" (you can still do that, by the way, if you wish). Seeing your code however, there seems to be some redundancy. You create iVars, and then, you create IBOutlets and then you synthesise the outlets with the name of the iVars. It's not an error, it's not even a warning, but you could save so much time by writing only the "@property" elements and not anything else, that I though I might just throw it as an option :)

csotiriou
  • 5,653
  • 5
  • 36
  • 45