Adding a ViewController to Appdelegate - uiviewcontroller

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

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

The best steps to work out with this are
Create your own delegate and make an instance in the AppDelegate
eg.
#protocol MyAppDelegate <NSObject>
#optional
- (void)myApplicationWillResignActive:(UIApplication *)application;
#end
Make a call's of your delegate in the MyAppdelegate
eg.
- (void)applicationWillResignActive:(UIApplication *)application {
[myDelegate myApplicationWillResignActive:application]
}
Implement it in your viewController class (ViewController).
4.assign you viewController class instance to myAppdelegate instance in AppDelegate
eg.
MyAppDelegate*myDelegate=(MyAppDelegate*)[[UIApplication sharedApplication] delegate];
myDelegate=self; //self=viewController class

Related

How to pass some variable to parent view controller?

I have a UIViewController its my main view controller. When the app starting that main viewcontroller is loading. Then there is a bar button. When I click on that, another view controller loding like this.
LoginViewController *viewController = [[LoginViewController alloc]
initWithNibName:#"LoginViewController" bundle:nil];
viewController.mainViewController = self ;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
navController.navigationBar.tintColor = [UIColor blackColor];
[self presentModalViewController:navController animated:YES];
In that new view controller I am doing some tasks and when click the close button, the results should be loaded in on the parent view controller.How can I do this. In parent view controller ViewDidAppear also not calling when I just close the top view controller. How can I do this? Please help me.
Thanks
First of all the line of code
[self presentModalViewController:navController animated:YES];
is deprecated and its been a long time, I don't know why are you still using it.
// Use below code instead
[self presentViewController:viewController animated:YES completion:nil];
Also you need to use protocols and delegates to pass any object to parent viewcontroller while you are dismissing the other viewcontroller.
If you can show up some code we might be able to figure out that why your viewDidAppear is not calling.

returning to a uiviewcontroller without navigationcontroller

my application has a UIViewController that isn't wrapped in a UINavigationController from it I call different UINavigationControllers each representing a state in a state machine.
calling these UINavigationControllers from code is done like this:
-(void) callState1
{
[popover dismissPopoverAnimated:YES];
UINavigationController *state1NavigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"state1Navigation"];
[self presentViewController:state1NavigationController animated:NO completion:nil];
}
in state1 there is a custom UIBarButtonItem representing the back control.
self.navigationItem.leftBarButtonItem = self.backBtn;
Is there a native code that returns to the calling UIViewController?
or should I implement the same code from callState1 in the back button calling the UIViewController, and if so how can I synchronize the data between them?
Since you are presenting the view controller (state1NavigationController) modally, you need to use the method dismissViewControllerAnimated:completion: to dismiss the state1NavigationController and go back.

How do I 'load' a storyboard with a specific id to an UIViewController?

Below is my code:
UIViewController *viewController = [UIViewController new];
if (indexPath.section == 0){
UIViewController *controller;
switch (indexPath.row) {
case 0:
controller = [[self storyboard] instantiateViewControllerWithIdentifier:#"ProfileSettings"];
[[viewController view] setBackgroundColor:[UIColor greenColor]];
All I want is to 'load' controller into viewController but I am not sure if it's possible.. can any of you explain what I did wrong?
I'm not sure why you would need to first create a new view controller, then instantiate another view controller and place it inside the view controller you initially created.
It would be better to do something like this:
id viewController;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
viewController = [[self storyboard] instantiateViewControllerWithIdentifer:#"ProfileSettings"];
// etc etc
// Display view controller as required
...however, if what you're actually trying to do is place your instantiated storyboard view controller inside of another view controller then you'll have to use view controller containment, which is available from iOS 5 onwards. View controller containment works like this:
[vc addChildViewController:instantiatedController];
[vc.view addSubview:instantiatedController.view];
But I'm not sure whether you really need to do that. It looks from the code you posted like there's no real need to use containment.

Need right direction for reload application by action

My scenario is a UIView Controller displays different subview programmatically... Like a game board. Now I'm in a incredible situation:
I can't find a way to reload application after the game is finish!? What I would like to do is after terminate scenario clic on IBAction for star new game. My doubt is about ViewDidLoad or call Main.
try moving your code from viewDidLoad into a new function like -(void)startGame or -(void)refreshViews: and call that function whenever you want to reload your view. even in viewDidLoad you can load your view using this function.
-(void)viewDidLoad
{
[super viewDidLoad];
[self startGame];
}
remember if you are creating allocating and add some of your subviews outside this function then they will remain visible untill and unless yu remove them from superView before calling startGame.
I solved it with this code:
(IBAction)newGame:(id)sender{
UIView *parent = self.view.superview;
[self.view removeFromSuperview];
self.view = nil; // unloads the view
[parent addSubview:self.view];
}

presentModalViewController from TableView

I have created a custom cell with buttons.
the buttons should present a modalViewController, one for the email and another for a webView.
I have also created a delegate to get a callback from the class.
so in the cell class:
_delegateAction = [[HistoryVC alloc] init];
_delegateAction.delegate = self;
[_delegateAction openData:_data Type:_title.text]; //start process
The class that the table is on is a UIviewController that have a table view. the (with the custome cells that are another class inherit from UiTableViewCell.
if you need meed information just tell me.
in the UIviewController I am trying to present a modal view as follow:
-(void) openData: (NSString *)data Type:(NSString*)type
{
QRWebView * webView = [[QRWebView alloc] init];
webView.url = data;
[self presentModalViewController: webView animated:YES];
}
The modalview doesn't show. I know that I'm doing something wrong, but what is it?
Thanks in advance.
You have created a new view controller with _delegateAction = [[HistoryVC alloc] init]; but you haven't made it part of the controller hierarchy by presenting it in any way.
When you call [self presentModalViewController: webView animated:YES];, your self is the new controller and, since it's not in the hierarchy, it's not capable of presenting another controller.
I have managed to fix it by implementing the PresentModalView.
What I did is to create a showModal and DismissModal in the AppDelegate class.
Those method add to the Window the view I wanted by adding it as:
[Window addsubview TheView];
and for the Dismiss is removeFromSuperview.
I implemented an animation to fake the modalView animation.
to get to those methods I have used the shareApplication.
and it's works.