I have a common action sheet (in NSObject class) that is called from multiple classes. One of the actions is launching email. Everything works great when called from the first NIB loaded. However, after I switch NIBs using modal views, the email (its own modal view) will not appear in any of these (but all other actions work fine). I presume it is because they are modal views and not the viewController called by the App's delegate. I presume the "rootViewController" below is the issue. I'm stuck. Help appreciated.
-(void)SendEmail {
// all this NSObject to send emails
rootViewController = (UIViewController*)[(AppDelegate*)[[UIApplication sharedApplication] delegate] viewController];
// compose
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
//controller.mailComposeDelegate = self;
controller.mailComposeDelegate = self;
//format message
NSArray *recipientsArray = [[NSArray alloc] initWithObjects:#"support#A___.com", nil]; // email recipients must be in an array
[controller setToRecipients:recipientsArray];
[controller setSubject:[NSString stringWithFormat:#"A question about %#",stuff]];
//[controller setMessageBody:outputMutString isHTML:YES];
//send
if (controller) {
NSLog(#"email controller");
[rootViewController presentModalViewController:controller animated:YES];
}
}
Related
I am implementing email composer in app delegate.
I am not sure why , but i am getting an warning
"instance method presentModalViewController:animated not found"
only at the appdelegate,whereas, i am using the the same method in my other viewcontroller, which works smoothly.
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
if([MFMailComposeViewController canSendMail])
{
//Setting up the Subject, recipients, and message body.
[mail setToRecipients:[NSArray arrayWithObjects:#"abc#gmail.com",nil]];
UIImage *pic = [UIImage imageNamed:#"page0.png"];
NSData *exportData = UIImageJPEGRepresentation(pic ,1.0);
[mail addAttachmentData:exportData mimeType:#"image/jpeg" fileName:#"Picture.jpeg"];
[mail setSubject:#"dummy text"];
[mail setMessageBody:#"Please tell us what your concerns are and we will try to fix them" isHTML:NO];
//Present the mail view controller
[self presentModalViewController:mail animated:YES];
//release the mail [mail release];
}
[self presentModalViewController:mailer animated:YES]; this is giving warning and app gets crashed,when reach here.
Any suggestions.
[self presentModalViewController:mail animated:YES];
is UIViewControllers Method . Please check whether you have extended your class from UIViewController
#interface abc : UIViewController
#NiravPatel: Thanks. You gave me the clue. But i had to replace "mail" by "mailer" in my case.
So, I got it work under iOS7 with
[self presentViewController:mailer animated:YES completion:nil];
presentModalViewController is a method for UIViewController's subclass and not for appDelegate, since it do not have any view controller of itself.
What you can do is, present it to last view controller of navigation controller if you are using that
Why implement this in your app delegate. I would just make a separate view and put whatever your AppDelegate is doing on the view and then implement this code in the viewcontroller. Trying to implement code like PresentModalViewController is not possible in a AppDelegate. Just create a new view and point the app delegate to load that view on startup.
presentModalViewController:animated: won't work because that's method of UIViewController but self here is an app delegate.
Refer View Controllers link for more reference
it is because that presentModalViewController is deprecated in ios 6.so for ios 6 you have to write the method like below
[self presentViewController:mail animated:YES completion:nil];
let me know it is working or not...!!!!
Happy Coding!!!!!!
I've set up a void method which is called from another class if the user touches the button.
However, I got this error with "whose view is not in window hierarchy" and then I found this: whose view is not in the window hierarchy. I just want to call the method below but I got this error and it doesn't show me the navigatoncontroller. It says Warning: Attempt to present <UINavigationController: 0xae34f10> on <ViewController: 0xae1f200> whose view is not in the window hierarchy!
I don't know if this is possible to call the method from the viewDidAppear or from somewhere that it only works if the button was tapped on the other view before. If I call a NSLog in the method it shows me the NSLog in the output. Only the presenting of the navController doesn't seems to work. I would be happy if someone could help me with this.
My method:
- (void) launchGalleryView
{
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set browser options.
browser.wantsFullScreenLayout = YES;
browser.displayActionButton = NO;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browser];
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"calculator" ofType:#"jpg"]];
photo.caption = #"The calculator is soo beauteful...";
[photos addObject:photo];
self.photos = photos;
[self presentModalViewController:navController animated:YES];
}
Just forgot to say: The MWPhotoprowser is a class which actually does't matter.
Thanks in advance.
The error message is pretty clear -- you're trying to present the controller from this class, but the class that has the button you pressed is the one whose view is in the view hierarchy. You either need to present it from the class with the button, or get back to this class however you're doing that, and then call it after viewDidAppear.
i checked your project.. wasn't able to sort out the proper issue..
but i tried a hack and it worked..
replace this line with
[self presentModalViewController:navController animated:YES];
this
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:navController animated:YES];
I am writing an iPad app and want to put an option on the home screen (root view controller) to allow users to e-mail feedback. I would like the mail to appear with the style "UIModalPresentationFormSheet". However, when I run the code, it appears full screen. What am I doing wrong? Here is the code I am using:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *toAddresses = [[NSArray alloc] initWithObjects:#"user#domain.com", nil];
[picker setToRecipients:toAddresses];
[toAddresses release];
[picker setSubject:#"App Feedback"];
self.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:picker animated:YES];
[picker release];
You must set the style of the viewController being presented, not on the one presenting it.. So set the property of the MFMailComposeViewController and you should be ok.
You need to set modalPresentationStyle on your new view controller, not self.
So I am writing an app that will read a JSON feed. In my application:didFinishLaunchingWithOptions, I am writing some code to download the JSON string and store it into a local NSString variable. I will then pass that string into ListingsViewController (which is the Root VC of the NavigationController). When I print out the JSON data in ListingsViewController, it is showing me (null) which is making me think that viewDidLoad is loading before - which seems illogical?
So here is my application:didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Grab the feeds
NSURL *jsonURL = [NSURL URLWithString:#"http://www.shoofeetv.com/iphonexml/view/all_channels.json"];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
// Pass jsonData to the ListingsViewController
ListingsViewController *listingsViewController = [[ListingsViewController alloc] initWithNibName:#"ListingsViewController" bundle:nil];
listingsViewController.jsonData = jsonData;
[listingsViewController release];
// Display the navigation controller
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
My viewDidLoad method is as follows:
- (void)viewDidLoad {
self.navigationItem.title = #"Listings";
UIBarButtonItem *checkinButton = [[UIBarButtonItem alloc]
initWithTitle:#"Check In"
style:UIBarButtonItemStylePlain
target:self
action:#selector(switchView)];
self.navigationItem.rightBarButtonItem = checkinButton;
[checkinButton release];
NSLog(#"%#", self.jsonData);
[super viewDidLoad];
}
Please note that a common solution is to make sure that the App Delegate in MainWindow.xib must be connected to the File's Owner. Mine already is connected.
I will appreciate any help!
Thank you everyone.
well you are setting up a view controller with your code, but it is never shown within the navigation controller. you just set up a view controller, assign its jsonData a string and destroy the view controller immediately. I#m pretty sure the output you get is from a different view controller that you create in your main XIB.
what you want to do is create an empty navigation controller in your XIB and then do the following:
// Pass jsonData to the ListingsViewController
ListingsViewController *listingsViewController = [[ListingsViewController alloc] initWithNibName:#"ListingsViewController" bundle:nil];
listingsViewController.jsonData = jsonData;
[self.navigationController pushViewController:listingsViewController animated:NO];
[listingsViewController release];
// Display the navigation controller
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
this will actually display the view controller you created.
Also remember that when you deploy your application, you need to load your json-data asynchronously and handle network errors (apple will test your app under various network conditions)
i'm new with iPhone developpement and i try to lunch a UIviewController when the user pressed a button witch will allow the user too send an email.
So my AppControler is a NSObject witch contains a UIWindow and a UIViewController* myViewController
When i detect the click on the button i create this:
MFMailComposeViewController *picker =
[[MFMailComposeViewController alloc] initWithNibName:#"MainWindow"
bundle:nil];
picker.mailComposeDelegate = myViewController;
... set the mail
and then when i try to present the view controller with this
[myViewController presentModalViewController:picker animated:YES];
[picker release];
Nothing append..
i know it's simple but i cant figured it out what is wrong.
thanks
Try using:
[self presentModalViewController:picker animated:YES];
As #dannywartnaby mentioned unless myViewController points to the current view controller (self) it won't work.