I have two tab controllers on my delegate and I would like to switch them appear as needed. RootController is displayed as default and when I would like to display secondController from another class I use,
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:appDelegate.secondController.view];
[appDelegate.rootController.view removeFromSuperview];
problem is when I am trying to go back using same method. Crashes due to bad acces exception...
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:appDelegate.rootController.view];
[appDelegate.secondController.view removeFromSuperview];
Any help would be appreciated. Thank you.
You should not remove the rootController From Superview. Remove that line
[appDelegate.rootController.view removeFromSuperview];
and when you want to go back to your rootController just add the following lines:
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.secondController.view removeFromSuperview];
Related
I'm having to class one which extends NSObject and second the view controller.
What is my problem is that I want to go to NSObject to view controller that loads my xib file of that view controller.
I have used the traditional, with no success thus far.
Suppose NSObject is MagentoLogin.m and I want to go to viewController, how can I achieve this?
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController presentViewController:myNewViewController animated:YES completion:nil];
try this hope it will help
how to call an AppDelegate method from RootViewController without passing a delegate?
Just wondering whether there is a way to do this? (or do I need to create a delegate object in the RootViewController to hold a reference to the AppDelegate)
[[[UIApplication sharedApplication] delegate] someMethod];
Works like a charm!
You can get access to the app delegate from any controller using
MyDelegate* aDelegate = (MyDelegate *)[[UIApplication sharedApplication] delegate];
This happens so frequently, that I always add a method to MyCustomAppDelegate to do this for me (so I don't have a lot of casting in my code.
#implementation MyCustomAppDelegate ()
- (MyCustomAppDelegate *)appDelegate {
return (MyCustomAppDelegate *)[[UIApplication sharedApplication] delegate];
}
#end
now anywhere i can call
[MyCustomAppDelgate appDelegate]
i am using a UINavigationController in my application.
in the first view i am displaying some information and have a button for loading an picture from the camera. this works fine so far. when the picture was loaded, i want to display the picture in a UIImageView within a second view. Therefore i am using the following code.
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
CorrectedImageController *imageController = [[CorrectedImageController alloc] initWithNibName:#"CorrectedImageView" bundle:nil];
self.correctedImageViewController = imageController;
[imageController release];
[delegate.navController pushViewController:imageController animated:YES];
The second view is loaded and the picture is shown. But i get an EXC_BAD_ACCESS message in the Debugger Console and my Application blocks the UI.
In the second view the picture is loaded as follows:
- (void)viewDidLoad {
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[imageView setImage:delegate.correctedImage];
[super viewDidLoad];
}
Can anybody tell me, what i did wrong ?
My nib file looks:
-File's Owner (Corrected Image Controller)
-First Responder (UIResponder)
-View (UIView)
--ImageView (UIImageView)
The File's owner view property is connected to the view.
And the imageView property is connected to the ImageView.
Can anybody tell me, what i did wrong ?
BR,
Martin
I believe this is because you are calling release on the imageController (line 5) and then trying the pushModalView the object you just released (line 6). If your self.correctedImageViewController is retaining the imageController, you could push the self.correctedImageViewController or release the imageController after the push.
So I have a UIWebView implemented in one .m file and a UITableView implemented in another. When a user clicks UITableView element, the UIWebView appears. The problem is that I have to set UIWebView's content (local text and images) depending on the UITableView's row number. I know how to do this, but this, of course must be implemented with the UITableView but it also needs UIWebView.
Just importing the UIWebView implementation does'n work.
Thanks in advance!
see this link to for setting delegates here
In didSelectRowAtIndexpath
NSString *name=#"content of table";
myAppDelegate *appDelegate = (myAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate setCurrentTitle:name];
PictureWebViewController *webview = [[PictureWebViewController alloc] initWithNibName:#"PictureWebViewController" bundle:nil];
[self.navigationController pushViewController:email animated:YES];
the PictureWebViewController is class of your webview .
In that class in viewdidload method get the title as
myAppDelegate *appDelegate = (myAppDelegate *) [[UIApplication sharedApplication] delegate];
NSString *name= [appDelegate getCurrentTitle];
Use the name in this class to display the content in webview.
All the Best
I have an AppDelegate which has 3 views. I add all three
[window addSubview:gameViewController.view];
[window addSubview:viewSettings.view];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
In the app delegate, i have some methodes for swapping views by calling
[window bringSubviewToFront:gameViewController.view];
When i am inside viewController, I use
pinkAppDelegate *appDelegate= (pinkAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate switchToSettings];
to switch my subviews...so far so good.
BUT, when I'm in my viewSetting UIViewController, and do the same appDelegate call, it chocks, like it doesn't understand how to call the appDelegate method.
I've got all my views hooked in my mainwindow xib, but can't figure out why i can't traverse the methods in the main appdelegate
Inside viewController, you're setting up a local variable called appDelegate that points at your app delegate. That's what this line does:
pinkAppDelegate *appDelegate= (pinkAppDelegate *)[[UIApplication sharedApplication] delegate];
This variable is local to viewController, so you can't use it in the settings view controller. You need to set up another variable there.
Alternatively, use this nice #define throughout your app. (You'll need to put it in a .h header file that you include in every file in your project.)
#define myAppDelegate (MyAppDelegate *)[[UIApplication sharedApplication] delegate]
Then you can do the following anywhere:
[myAppDelegate doSomething];
For reference there is still a typo in the above, also I have another addition which helps avoid warnings of having to cast every time. if you use:
#import "MyAppDelegate.h"
#define myAppDelegate (MyAppDelegate *) [[UIApplication sharedApplication] delegate]
I put the above in a constants.h and then can use
[myAppDelegate doSomething];
anywhere I import constants.h
Jane's answer had a typo in it.
Use the following line in a header file (e.g. defines.h) that you import in every class:
#define myAppDelegate [[UIApplication sharedApplication] delegate]
You could also use the line in a prefix header file - this is an easy way of getting access to this everywhere.
then in any method you can use any of the following:
[myAppDelegate doSomething];
myAppDelegate.property=value;
myAppDelegate.childClassOfMyAppDelegate.property=value;
[myAppDelegate.ChildOfMyAppDelegate doSomething];