Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a ViewController to Appdelegate

I am trying to add a View Controller to the Appdelegate Class. My code goes this way..

[self.view addsubView:viewcontroller.view];

But unfortunately i am not able to view the controller view. Please suggest if i am going wrong somewhere.Thanks for ur time.

The method in AppdidFinishLaunching is:-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    // [self.window addSubview:videoController.view];
    self.window.rootViewController = videoController;
    [self.window makeKeyAndVisible];    
    return YES;
}

and in the Load view of ViewController i have written as

UIButton *playMovie = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playMovie.frame = CGRectMake(70,30,100,50);
[playMovie setTitle:@"Play Movie" forState:UIControlStateHighlighted];
[playMovie addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:playMovie];

The problem here is i am not able to view the button and the view.Please help.

like image 415
Pradeep Reddy Kypa Avatar asked Mar 04 '12 17:03

Pradeep Reddy Kypa


People also ask

What is the difference between AppDelegate and SceneDelegate?

AppDelegate is responsible for handling application-level events, like app launch and the SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession.

Is AppDelegate a controller?

The application delegate is a controller object. By default, it is the owner and controller of the main window -- which is a view -- in an iOS app. The app delegate receives messages from an object representing -- or modeling -- the application itself (an instance of UIApplication ).


2 Answers

Under no circumstances should you ever add a view controller's view manually to the interface like this. The app delegate is not, itself, a view controller; so viewcontroller is not its child. So the view is not yours to add. Use view controllers correctly: a view controller is either your app window's rootViewController, or it is some other view controller's child - and in either case, its view is placed into the interface for you automatically.

If this view is intended to be the root view of the app, then do what the Xcode 4.2 project templates do (e.g. the Single View Application): instantiate the view controller and assign it as self.window.rootViewController. If not, you must not use a view controller to add it as a subview as you are trying to do; just obtain the view and add it, without the intervening view controller.

It might help you to read my chapter on view controllers: http://www.apeth.com/iOSBook/ch19.html

like image 145
matt Avatar answered Sep 28 '22 07:09

matt


Try below it works for me,

SFRmbrVC *viewController=[[SFRmbrVC alloc]initWithNibName:@"SFRmbrVC" bundle:nil];
[self.window.rootViewController addChildViewController:viewController];
viewController.parentController = self.window.rootViewController;
viewController.view.frame = self.window.rootViewController.view.frame;
[self.window.rootViewController.view addSubview:viewController.view];
viewController.view.alpha = 0;
[viewController didMoveToParentViewController:self.window.rootViewController];

[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^
 {
     viewController.view.alpha = 1;
 }
                 completion:nil];
like image 27
ajay_nasa Avatar answered Sep 28 '22 05:09

ajay_nasa