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.
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
viewcontrolleris not its child. So the view is not yours to add. Use view controllers correctly: a view controller is either your app window’srootViewController, 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