Dismiss modal view controller on application exit - objective-c

I have a view controller (view A) presenting a modal view (B) when the user pushed a button and the view B has itself a button to present view C. My problem is that if the user exits the application when the view B or C is shown, the same view will appear next time the application is launched. Is there a way to dismiss the views B and C on exit or to show view A when the application starts?
Thanks for your help

I assume by close you mean when the application enters the background.
In your app delegate you can via the applicationDidEnterBackground: method dismiss your controller.
Best way would probably be to add an observer in your view controller class:
- (void) viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appClosing) name:#"appClosing" object:nil];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"appClosing" object:nil];
[super dealloc];
}
- (void) appClosing
{
[self dismissModalViewControllerAnimated:YES];
}
And post the notification in your app delegate:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"appClosing" object:nil];
}

Related

How to change set title uibutton?

UIButton *loginButton = [self.loginViewController LoginButton];
loginButton.titleLabel.text=#"Log out";
//[loginButton setTitle:#"Log out" forState:UIControlStateNormal];
NSLog(#"Log in :-%#",loginButton.titleLabel.text);
1) i have view controller file that has one button and i want change button title when didFinishedLaunching method called from app-delegate.
i also initialized the controller but that has no change.
thank in advance..
To set the title correctly:
[loginButton setTitle:#"Log Out" forState:UIControlStateNormal];
edit: If you are looking to update view states after a change from the AppDelegate, it would be a good idea to look at using NSNotifcationCenter. In the app delegate, you can post a notification about the user logging in or out, and then you can configure your viewController to be an observer for the notification and update its state when the notification is made.
For example, in your app delegate
- (void)userDidLogOut
{
//This method would be called when you logout
[[NSNotificationCenter defaultCenter] postNotificationName:#"didLogoutNotification" object:nil];
}
Then in your loginViewController
- (void)viewDidLoad
{
//...
//Become an observer of `didLogoutNotification`.
[[NSNoficationCenter defaultCenter] addObserver:self selector:#selector(didLogoutNotification:) name:#"didLogoutNotification" object:nil];
}
- (void)dealloc
{
//...
//Remove yourself from the observation list.
[[NSNoficationCenter defaultCenter] removeObserver:self];
}
- (void)didLogoutNotification:(NSNotification *)notification
{
//...
//Update the button
[loginButton setTitle:#"Log In" forState:UIControlStateNormal];
}

objective-c reload UITableView after back navigation

I have a TableViewController embedded in a NavigationController, at some point I use a Segue to push to a new ViewController. When I navigate back from the ViewController I want to reload the tableView in the TableViewController.
I followed the solution posted here and here. For some reason it does not work. I miss something but I can't see it at the moment.
Has anybody a glue, why the code does not work? What do I have to do to get it work?
TableViewController.m
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// notification when comeing back from the cameraView
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadMagazine:) name:#"reloadTable" object:nil];
}
return self;
}
//[...]
// notification reload
-(void)reloadMagazine:(NSNotification *)notification {
NSLog(#"notification reload");
[self.tableView reloadData];
}
//[...]
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadTable" object:self];
}
ViewController.m
-(void)uploadData:(id)sender {
// Upload and navigate back to the tableViewController
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadTable" object:self];
[self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
This does not answer your question "why the code does not work?", but the easiest
method to reload the table view when navigating back to it could be to
call reloadData in viewWillAppear.
I suspect that the code above does not work because initWithStyle is not being called. Put a breakpoint there to test my theory. If that is the case, then move that code to viewDidLoad.

How to close a modal view when the application enter in background on ios

I have a modal view created in a method (there is no reference in the mainview) and I want to do a dismissModalViewControllerAnimated automatically when my app enter in background. How can I do that ?
In the mainview's viewDidLoad, add observer to be notified when app goes to background.
- (void) viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(goToBackground)
name:UIApplicationWillResignActiveNotification object:nil];
}
Define the function goToBackground(). It will be called when the app goes to background
- (void) goToBackground
{
[self dismissModalViewControllerAnimated: NO]; // no need to animate
}
Don't forget to remove the observer
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
You can use a notification. Post a notification from the ApplicationDelegate's method applicationDidEnterBackground:. YOu can call the dismiss method from the modal controller, so add it as observer to the notification center.

How to reload the tableView after dismissing a NavigationController in uisplitviewcontroller?

I'm using a UISplitviewController as a template.
action for edit button:
newExViewController *editWindow =[[newExViewController alloc]initWithNibName:#"newExViewController" bundle:nil];
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:editWindow];
navBar.modalPresentationStyle = UIModalPresentationFormSheet;
navBar.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navBar animated:YES];
[navBar release];
[editWindow release];
navBar has a UIBarButton for saveButton. This is called when you press SaveButton
[self dismissModalViewControllerAnimated:YES];
now is the problem:
any idea how to reload the data for both the main NavigationConteroller and the detailViewController when the modalView is dismissed??
I have no clue
thnx
You should look into NSNotificationCenter. In your view with the UITableView, create the notification listener. Then in the view that dismisses, call that notification.
To be more specific, the Notification will call a method that should contain reloadData.
Example
The following should go with the UITableView you want to reload:
This could go along with your [self dismissModalViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(someMethodToReloadTable) name:#"reloadTable" object:nil];
This is how you will call the notification center to reload the table:
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadTable" object:self];
Example of the notification method:
- (void)someMethodToReloadTable:(NSNotification *)notification
{
[myTableView reloadData];
}
And don't forget to remove the notificaiton observer:
-(void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"reloadTable" object:nil];
}
In controllers, that contains view you want to reload, you should decline following method which will be called when the modalView will be dismissed (or when controller's main view will be first time loaded):
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// here you can reload needful views, for example, tableView:
[tableView reloadData];
}

NSApplicationWillTerminateNotification not Working

My problem is that the NSApplicationWillTerminateNotification is not called when I quit my application. What I have tried: (appDelegate.m)
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification{
NSLog(#"quit");
}
OR
- (void)applicationWillTerminate:(NSApplication *)application{
NSLog(#"Quit");
}
My application has no window, as it is a background application -> I deleted the window and the menu in interface Builder.
Apps in the background are terminated without any notification.