I'm building an app use bluetooth for communication, and I have a class called EAController(NSObject) to handle the accessory delegate.
So if the phone lost bluetooth communication, an accessory delegate method is called in EAController where I can show an alert. What I want to do is to when lost communication, show an alert and go back to the initial view controller.
I know I can add a notification, and have all the view controllers listen to that, and the current view controller can take care of the "Go Back to First View Controller" action.
But my app has so many view controllers, so is there any way I can go back to the initial view controller through the EAController class?
Thanks.
Handle the notification in your app delegate.
Your explanation was a little vague, some code might help me understand the problem better but it sounds like UINavigationController popToRootViewControllerAnimated: might be what you are looking for.
If EAController is a UIViewController, and has been added a navigation controller, it should have a reference to the nav controller to call the method.
If you're using a UINavigationController you could us the
-popToRootViewControllerAnimated: method
If you are working with storyboards and segues you should look into unwind segues. There are several good tutorials on the net., eg.
http://cocoatouch.blogspot.nl/2013/02/unwinding-segues-in-storyboard.html
if you are using UInavigationController:
for(UIViewController* vc in self.navigationController.viewControllers) if([vc isKindOfClass:FirstViewController]
{
[self.navigationController popToViewController:vc anmated:YES];
break;
}
popToRootViewControllerAnimated: will do what you want. If you are in a part of the code where you do not have direct access to the UINavigationController you can push an event and configure your navigation controller to receive it. Like so:
Where you have your navigation controller:
UINavigationController *navController = [[UINavigationController alloc] init];
// subscribe to the notification
[[NSNotificationCenter defaultCenter] addObserverForName:#"goBack!"
object:nil
queue:nil
usingBlock:^(NSNotification *notification) {
// pop!
[navController popToRootViewControllerAnimated:YES];
}];
Now, somewhere else in the code... do this when you want to go back:
// post the notification
[[NSNotificationCenter defaultCenter] postNotification:
[[NSNotification alloc] initWithName:#"goBack!"
object:nil
userInfo:nil]];
Related
Scenario:
I need to show 3 or more popups one after the other on button click in each popup. I have created a different viewcontroller and xib files for each popup. So for displaying each popup I have used presentViewController instead of pushViewController.
That is, I have used this:
[self presentPopupViewController:searchPopUpView animationType:0];
instead of
[self.navigationController pushViewController:searchPopUpView animated:YES];
For dismissing a popup, the following code has been written:
[self dismissPopupViewControllerWithanimationType:0];
Issue:
The popups are displaying perfectly, but the background gets darker and darker whenever a popup shows up. After all popups have been dismissed I have to finally click on the blank screen to remove those darker parts. How to overcome this issue?
I think you are using MJPopupViewController to show pop-up.
If it is so, Then try this.
Suppose there is a controllerA from which you want to show a pop-up controller popupControllerB.
Then in your controllerA add Notifications Observer
Code to write in controllerA :
// Add Notification Observer when your view initialise.
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(dismissPopup) name:#"DISMISS_POPUP" object:nil];
In viewWillDisappear remove the notifications observer
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
This method will be called when you Post-notification from your popupControllerB
-(void)dismissPopup {
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}
And In popupControllerB, Where you want to dismiss the Pop-up, write this code.
[[NSNotificationCenter defaultCenter] postNotificationName:#"DISMISS_POPUP" object:nil];
Above line of code will call a method written in your controllerA and dismiss the pop-up properly.
If you want to dismiss presented UIViewControllers you can use this code. I have used this approach to dismiss presentedViewControllers. It will dismiss all your presentedViewControllers on your rootViewController.
UIViewController* presVC = self.window.rootViewController;
while (presVC) {
UIViewController* temp = vc.presentingViewController;
if (!temp.presentedViewController) {
[vc dismissViewControllerAnimated:NO completion:^{}];
break;
}
vc = temp;
}
I need to go back all the way to the view controller that presented the first navigation controller. However I haven't dismissed multiple controllers before at once, and when I've tried doing so, it doesn't work. It just goes to the first navigation controller instead of all the way to the one before it.
Here is my current code:
[(UINavigationController *)self.presentingViewController popViewControllerAnimated:NO];
[self dismissViewControllerAnimated:YES completion:nil];
I have a view controller which modally presents the first navigation controller. The first navigation controller screen is called Main View Controller. It then pushes to Login View Controller. Login View Controller does presentViewController to MenuViewController (UIViewController).
I need to get from MenuViewController all the way back to the view that presented the first navigation controller. Thanks.
Try this
UIViewController *vc = self;
while (vc.presentingViewController != nil) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:nil];
One option would be to use NSNotifications.
You can add an observer in your first/root/initial UINavigationController subclass e.g.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(logout:)
name:#"LogoutNotification"
object:nil];
Then in your "logout:" method you have direct control over the initial UINavigationController rather than those further up the hierarchy.
You can then send an NSNotification from anywhere in the app in order to trigger the method.
e.g.
[[NSNotificationCenter defaultCenter] postNotificationName:#"LogoutNotification" object:self];
I have a UITabBarController with four tabs. In each of the view controllers presented when a tab is selected I have a reset button. Tapping the button will change the appearance of all the view controllers. In particular, it will change the text of some labels in the different view controllers.
Is there some recommended way to update all the view controllers of a UITabBarController at the same time i.e. to make them reload their views?
My current approach is to make those view controllers conform to a protocol
#protocol XYReloadableViewController
- (void)reloadContents;
#end
and then send the message -reloadContents to all the view controllers when the button is tapped:
- (IBAction)touchUpInsideResetButton {
// ...
NSArray *viewControllers = self.tabBarController.viewControllers;
for (UIViewController<XYReloadableViewController> *viewController in viewControllers) {
[viewController reloadContents];
}
}
Then in each of the view controllers I would have to implement that method:
- (void)reloadContents {
[self.tableView reloadData];
// other updates to UI ...
}
But that seems a little too complicated. So is there an easier way to tell the view controllers to reload their views?
Edit: And what happens if I present a UINavigationController in some of the tabs or a container view controller? I would need to pass the message along the chain of all its child view controllers...
You can create ReloadViewController and all you contrlollers inheritance
from him.
ReloadViewController have property UIButton and methods:
-(void)reloadContents;
-(IBAction)touchUpInsideResetButton:(id)sender;
in .m file:
-(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadContents)
name:#"MyNotification"
object:nil];
}
- (IBAction)touchUpInsideResetButton:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification"
object:nil];
}
in your viewControllers need only override method reloadContents
Notifications sound like a better fit for this. When view controllers need to be reset, broadcast an NSNotification and have any view controllers that might need to reset themselves listen for that notification, and trigger what they need to do. That way it doesn't matter how far down a navigation stack they are.
You might want to defer updates until the view actually appears. You could set a BOOL needsUpdate when the VCs receive the notification, but only do the actual update in viewWillAppear:, to save resources and prevent a large number of updates from going off at once (and perhaps blocking the main thread).
If this behaviour is common to all your view controllers, make a UIViewController subclass to prevent repeating code and have them all inherit from that. Alternatively, (if you're using Apple VC subclasses) make a category on UIViewController to add the notification methods.
I currently have written a slider (similar to the Facebook app) for my app. At the top of the slider is a Search Box, and the methods controlling the search functionality are also within the app delegate.
Similarly, I have the methods that control the slider's table view in a separate class (SliderMenuViewController).
I am looking for a way for the slider (either the search box or the tableview cells) to be able to tell the RootViewController (or whichever viewController is currently visible) to push a new ViewController (inside a UINavigationController).
This is what I tried to do (this code is in the AppDelegate):
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(#"Searching for: \"%#\"",searchBar.text);
[searchBar resignFirstResponder];
IndexAndSearch *vc = [[IndexAndSearch alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
But it doesn't work (it writes to the log, but doesn't push the new ViewController). I also tried sending a message to the RootViewController like this:
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(#"Searching for: \"%#\"",searchBar.text);
[searchBar resignFirstResponder];
RootViewController *vc = [[RootViewController alloc]initWithNibName:nil bundle:nil];
[vc performSearchFromDelegateSlider];
}
With the following code in the RootViewController's implementation file:
-(void)performSearchFromDelegateSlider{
NSLog(#"Searching");
IndexAndSearch *vc = [[IndexAndSearch alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
But once again it only wrote to the log, not pushing a viewController.
I've looked far and wide on Google and SO, but haven't been able to find anything useful. This question is similar to mine, but there haven't been any suitable answers. I know the answer probably involves delegation, but I can't wrap my head around a solution for this.
Important note: This slider is available from nearly every ViewController in the app, meaning that whatever solution I implement has to be able to push a new ViewController for every class. That is why I can't use a solution like this one (I would have to enter the NavigationDelegate code into each ViewController, which won't work in an app as large as mine).
Thanks in advance for your help guys.
I'm not convinced it is the best solution, but I was able to get this working using notifications. For anyone that is interested, here is what I did:
Step 1
The first step is to register for the notification in the RootViewController's viewDidLoad method:
-(void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didReceiveNavSliderSearchNotification:) name:#"navSliderSearchNotification" object:nil];
}
Step 2
I then need to fire the notification when the search is performed from the slider. The searchBar code is located in my AppDelegate and looks like so:
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
//Write to the log
NSLog(#"Searching for: \"%#\"",searchBar.text);
//Dismiss the keyboard
[searchBar resignFirstResponder];
//Post the notification (to be used by the RootViewController
[[NSNotificationCenter defaultCenter] postNotificationName:#"navSliderSearchNotification" object:self];
}
Step 3
I then need to write the didReceiveNavSliderSearchNotification class (which will be called in the RootViewController when the navSliderSearchNotification notification is posted and received):
-(void)didReceiveNavSliderSearchNotification:(NSNotification *) notification {
if ([[notification name] isEqualToString:#"navSliderSearchNotification"])
NSLog (#"Successfully received the search notification!");
//Push the next ViewController when the *navSliderSearchNotification* is received
IndexAndSearch *vc = [[IndexAndSearch alloc]initWithNibName:#"IndexAndSearch" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
And that is how I managed to push new viewControllers from a separate class (in this case the App Delegate, but I also have it working from other classes as well).
Final step (optional)
My slider is accessible from everywhere in the app, so I did not unregister from my notifications in the RootViewController (meaning these methods will continue to fire even if the user has been pushed to another viewController). If you do not want this functionality, make sure to unregister from these notifications using the following code (it would go in the RootViewController):
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"navSliderSearchNotification" object:nil];
}
Like I said, I am not entirely convinced this is the best method. If you have another solution that you prefer, please feel free to post it.
Have you tried doing this:
Create a UIViewController variable in your AppDelegate that always references the current UIViewController on the screen (You might need to set the current controller every time you create a view controller)
Once that's all done.
In your AppDelegate use
[self.currentViewController.navigationController pushViewController:anotherNewViewController animated:YES];
I need to be notified about modal view controller presentation, I know that I can do using NSNotificationCenter posting and observing my custom notifications, but I'd like to know if these kind of notification already exist.
Thanks, Andrea
I've found a solution. I've just create an abstract class that overrides the methods for presenting a modal view, like that:
- (void) presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated{
[[NSNotificationCenter defaultCenter] postNotificationName:PresentingModal object:self];
[super presentModalViewController:modalViewController animated: animated];
}
Of course all my view controllers inherit from it.
Hope this helps.