How to clear Unsaved draft for contact in iOS? - ios

Open the default contact form to add a number
Add a number and click on cancel to remove and open new modal for confirmation
Select discard changes modal close
Now when I open the contact book then first open the modal with the last number I added in the contact book form which added from my app
here is my code:
[self updateRecord:contact withData:contactData];
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];
controller.delegate = self;
dispatch_async(dispatch_get_main_queue(), ^{
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:controller];
UIViewController *viewController = (UIViewController*)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (viewController.presentedViewController)
{
viewController = viewController.presentedViewController;
}
[viewController presentViewController:navigation animated:YES completion:nil];
self->updateContactPromise = resolve;
Below is delegate method code:
[viewController dismissViewControllerAnimated:YES completion:nil];
if(updateContactPromise) {
if (contact) {
NSDictionary *contactDict = [self contactToDictionary:contact withThumbnails:true];
updateContactPromise(contactDict);
} else {
updateContactPromise(nil);
}
updateContactPromise = nil;
}
}

Related

Why my viewWillAppear method is not calling everytime i go back to my parent view

This is my viewController.m file
- (IBAction)defaultAction:(id)sender {
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
[self presentViewController:simpleView animated:YES completion:nil];
}
- (IBAction)flipAction:(id)sender {
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
[simpleView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:simpleView animated:YES completion:nil];
}
- (IBAction)dissolveAction:(id)sender {
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
[simpleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:simpleView animated:YES completion:nil];
}
- (IBAction)pageCurlAction:(id)sender {
self.callFromPageCurl = true;
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
simpleView.delegate = self;
[simpleView setModalPresentationStyle:UIModalPresentationFullScreen];
[simpleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentViewController:simpleView animated:YES completion:nil];
}
- (IBAction)showWithDelegate:(id)sender {
self.callFromShowWithDelegate = true;
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
simpleView.delegate = self;
[self presentViewController:simpleView animated:YES completion:nil];
}
- (void)didReceiveMessage:(NSString *)message{
self.myMessage.text = message;
}
- (void)viewWillAppear:(BOOL)animated{
self.callFromShowWithDelegate = false;
self.callFromPageCurl = false;
NSLog(#"Called first");
}
and this is my simpleViewController.m file
- (IBAction)dismissMeAction:(id)sender {
if(self.delegate.callFromShowWithDelegate)
[self.delegate didReceiveMessage:#"Hello World"];
else
[self.delegate didReceiveMessage:#"My Message"];
if(self.delegate.callFromPageCurl == true)
[self dismissViewControllerAnimated:NO completion:nil];
else
[self dismissViewControllerAnimated:YES completion:nil];
}
When i use pageCurl to go to the next view and i came back to the main view it is calling viewWillAppear method again But if i go to the next view using any other button and came back to the main view it is not calling viewWillAppear method.. But why? SHouldn't it have called viewWillAppear method every time i came back to main view.
If you have used modal presentation for view controller prior to iOS 13, the default behavior is presenting the modal view controller in full screen, and when the modal view controller is dismissed, the viewDidAppear function will be called on the presenting view controller (ie. the view controller that is responsible for presenting the modal view controller).
However in iOS 13, the default behavior of presenting the modal view controller is replaced with a card-like animation (the official term for it is page sheet)
In iOS 13+, You need to set the correct presentationStyle. If you want that your presentedController will be fullScreen and call previous viewWillAppear, then you can use "UIModalPresentationFullScreen"
- (IBAction)defaultAction:(id)sender {
SimpleViewController *simpleView = [[SimpleViewController alloc]init];
[simpleView setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:simpleView animated:YES completion:nil];
}
You can change this in your nib/storyboard as well:

Fix Warning: Attempt to present ViewController on NavigationController whose view is not in the window hierarchy

Currently, I have set up a log-in view for users of my app. Below is the code that presents this log-in view to the user:
// Handle how we present the view
if (self.notificationToProcess != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWNotificationsViewController *viewController = [[NotificationsViewController alloc] init];
viewController.initialDataID = self.notificationToProcess[#"Data"];
self.notificationToProcess = nil;
[self.navigationController pushViewController:viewController animated:YES];
}];
} else if (self.detailURL != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
WebViewController *wvc = [[WebViewController alloc] init];
wvc.URL = self.detailURL;
wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:wvc animated:YES completion:nil];
self.detailURL = nil;
}];
} else {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
What I'm attempting to do now is display a web-view first if the user has just updated the app. Below is what this new code looks like:
// Handle how we present the view.
if (self.notificationToProcess != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWNotificationsViewController *viewController = [[SWNotificationsViewController alloc] init];
viewController.initialDataID = self.notificationToProcess[#"Data"];
self.notificationToProcess = nil;
[self.navigationController pushViewController:viewController animated:YES];
}];
} else if (self.detailURL != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWWebViewController *wvc = [[SWWebViewController alloc] init];
wvc.URL = self.detailURL;
wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:wvc animated:YES completion:nil];
self.detailURL = nil;
}];
else if (![versionOfLastRun isEqual:currentVersion])
{
SWWebViewController *webViewController = [[SWWebViewController alloc] init];
NSString *url=#"http://someurl";
webViewController.URL = url;
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:webViewController animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"VersionOfLastRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else if (versionOfLastRun == nil){
// First start after installing the app
}
else {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
However, when I run the app, the web view is not displayed, I get a warning of:
Warning: Attempt to present ViewController on NavigationController whose view is not in the window hierarchy!
Can anyone help me diagnose the problem? Thank you!
Seems like your viewController is not in Navigation controller hierarchy. This means that your controller is not connected with navigation controller stack which should handle presented controllers.
The other thing that should help is setting your root view controller as follows:
self.window.rootViewController = self.ViewController;
EDIT: since you mention that you don't use storyboards, setting rootViewController should do the trick.
Try to set the window's rootViewController to the navigation controller after you initialize the navigation controller:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;
This is how I'm doing it without storyboards.
The problem was that I didn't call self.navigationController dismissViewControllerAnimated:YES completion:^
After correcting this (see listing below), my WebView was shown as expected.
else if (![versionOfLastRun isEqual:currentVersion])
{
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWWebViewController *webViewController = [[SWWebViewController alloc] init];
NSLog(#"%#", self.window.rootViewController);
NSString *url=#"http://devstatwatch.drbsystems.com/releases_mobile.php";
webViewController.URL = url;
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:webViewController animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"VersionOfLastRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
}

How to return to view controller in main.storyboard from a xib without using navigation controller or navigation bar?

I am making an app with different view controllers. My home screen is in main.storyboard. Rest of the view controllers have their own xib, .h & .m files. I am trying this for navigation from home screen.
-(IBAction)btnSignUpTapped:(id)sender
{
SignUpWithEmailViewController * login = [[SignUpWithEmailViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:login];
[self presentViewController:nav animated:YES completion:NULL];
}
This code works fine for navigating to different viewcontroller (in this case SignUpwithEmailViewController). On SignUpWithEmailViewController I have a back button which is supposed to bring me back to home screen. This is what I got so far:
-(IBAction)btnBackTapped:(id)sender
{
ViewController * homeScreen = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc]
initWithRootViewController:homeScreen];
[self presentViewController:nav animated:YES completion:NULL];
}
But as result of this code, screen turns black and nothing happens. How do I solve this problem? For hiding the nav bar I am using
-(void) viewWillAppear:(BOOL)animated
{
[[self navigationController] setNavigationBarHidden:YES animated:NO];
}
Yes, you have to insert you called viewController to NavigationController,
but if you wont to use modal flow - to close presentedViewController just call:
[self dismissViewControllerAnimated:YES completion:^{
}];
EDITED:
may be it will be more helpful, it just example, put it to you main view controller:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIViewController *c = [[UIViewController alloc] init];
c.view.backgroundColor = [UIColor redColor];
[self presentViewController:c animated:YES completion:^{
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
}

dismissing a view controller and presenting another viewController

In my application, I have 4 UIViewcontrollers - A, B, C and D.
From my UIViewController A, UIVIewControllers B or C or D can be presented. Suppose, from A, i have presented B. Then, on B, there is are two UIButtons, clicking on which, i need to dismiss B and Present C or dismiss B and Present D. This is happening successfully, but first, after dismissing B, the screen of A comes, and then it goes to C or D.
Here's what i did:
On Button Press On B, the action is:
{
UINavigationController *controller = (UINavigationController *)self.parentViewController;
NSLog(#"%#",[controller parentViewController]);
UITabBarController *tabBarReference = (UITabBarController *)[controller parentViewController];
HomePageViewController *presController = (HomePageViewController *)tabBarReference.presentingViewController;
[presController dismissTabControllerWithHandlerWithSenderAtIndex:1];
}
In A, i have these methods:
-(void)dismissTabControllerWithHandlerWithSenderAtIndex:(NSInteger)index
{
if(index == 0)
{
[self dismissViewControllerAnimated:NO completion:^{
[self aboutButtonPressed:self];
}];
}
else
{
[self dismissViewControllerAnimated:NO completion:^{
[self settingsButtonPressed:self];
}];
}
}
Here's the method i am calling..
- (IBAction)settingsButtonPressed:(id)sender
{
[self.contactUsView setHidden:YES];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *deviceType = [prefs objectForKey:#"DeviceType"];
NSString *iOSType = [prefs objectForKey:#"iOSType"];
if([iOSType isEqualToString:#"iOS7"])
{
if([deviceType isEqualToString:#"iPad"])
{
SettingsViewController *settingsPage = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController_iPad" bundle:NULL];
settingsPage.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:settingsPage animated:YES completion:NULL];
}
else
{
SettingsViewController *settingsPage = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:NULL];
settingsPage.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:settingsPage animated:YES completion:NULL];
}
}
else
{
if([deviceType isEqualToString:#"iPad"])
{
SettingsViewController *settingsPage = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController_iPad_iOS6" bundle:NULL];
settingsPage.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:settingsPage animated:YES completion:NULL];
}
else
{
SettingsViewController *settingsPage = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController_iOS6" bundle:NULL];
settingsPage.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:settingsPage animated:YES completion:NULL];
}
}
}
Ok here goes:-
Suppose viewcontroller A is the base & it presents either view controllers B or C.
Implement delegate/notifications where the view controller B or C has to be dismissed.
After sending the delegate or posting the notification, dismiss the current view controller without animation, i.e animation:NO.
This notification that was posted needs to be heard in view controller A.
Here you present the next view controller.
EDIT:-
-(void)dismissTabControllerWithHandlerWithSenderAtIndex:(NSInteger)index
{
if(index == 0)
{
[self dismissViewControllerAnimated:NO completion:nil];
[self aboutButtonPressed:self];
}
else
{
[self dismissViewControllerAnimated:NO completion:nil];
[self settingsButtonPressed:self];
}
}

EKEventview controller not showing navigation bar

I used the following code to view the event. But navigation bar doesn't visible.
EKEventViewController *addController = [[EKEventViewController alloc] initWithNibName:nil bundle:nil];
addController.event = self.event;
addController.allowsEditing = YES;
addController.allowsCalendarPreview = YES;
[self.navigationController presentViewController:addController animated:YES completion:nil];
with Present viewcontroller you need to Add sepreated NavigationController for UIViewcontroller like:-
EKEventViewController *addController = [[EKEventViewController alloc] initWithNibName:#"EKEventViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:addController];
addController.event = self.event;
addController.allowsEditing = YES;
addController.allowsCalendarPreview = YES;
if ([self respondsToSelector:#selector(presentViewController:animated:completion:)])
{
[self presentViewController:navController animated:YES completion:nil];
}
else
{
[self presentModalViewController:navController animated:YES];
}

Resources