How to dismiss middle viewControllers and come back to rootViewController - ios

I have three view controllers, say root, middle, last. I want to go back from lastViewController to rootViewController.
I tried custom delegate and NSNotificationCenter. But while going back the middleViewController flashes for a second (it's viewWillDisappear gets called).
I'm using presentViewController/dismissViewController and not navigation. I don't want that middleViewController getting flashed.
I have below code in rootViewCotroller:
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(actionBackToRoot) name:#"BackToRoot" object:nil];
}
-(void)actionBackToRoot : (NSNotification *) notification{
NSDictionary *userInfo = notification.userInfo;
UIViewController *lastViewController = [userInfo objectForKey:#"viewController"];
[middleViewController dismissViewControllerAnimated:NO completion:nil];
[lastViewController dismissViewControllerAnimated:NO completion:nil];
}
On click of close button of lastViewController, code as below -
[dict setValue:self forKey:#"viewController"];
[[NSNotificationCenter defaultCenter]postNotificationName:#"BackToRoot" object:nil userInfo:dict];
Does anyone have solution for this?
Thanks in advance.

Did you tried lastViewController.navigationController.popToRootViewController(animated: true )

Related

Change Scrollbar background from another view IOS

This is HomeView
enter image description here
When Someone tapped in 'Banking' button . Action will load BaseviewController .
This is the button action.
HomeViewController.m
BaseViewController *clickbutton = [[BaseViewController alloc] initWithNibName:#"BaseViewController" bundle:nil] ;
[self presentViewController:clickbutton animated:YES completion:nil];
This is BaseView
enter image description here
There is a scrollbar which will load every view . The scrollbar
background is Green . And change the background in next here is the
problem .
BaseViewController.m
-(void)ViewDidLoad{
PriorityViewController *obj ;
UINavigationController *nav;
obj = [[ PriorityViewController alloc] initWithNibName:#"PriorityViewController" bundle:nil] ;
nav = [[UINavigationController alloc] initWithRootViewController:obj];
nav.view.frame = rect;
[self.view addSubview:self.nav.view];
}
When tapped 'Service Request' .
OtherBankTransferViewController *obj;
obj = [[OtherBankTransferViewController alloc]initWithNibName:#"OtherBankTransferViewController" bundle:nil];
[self.navigationController pushViewController:obj animated:YES ];
This will load same as like second image I uploaded here .
I just want to change my background color of the scrollbar
I want to change scrollbar into black color .
I have no idea . If someone explain me !
Thanks in advance .
APPROACH 1:
Instead of initialising scrollBar in each ViewController, why not alloc init it in BaseView Controller and pass instance of it to all View Controller.
Since now all ViewControllers will have same instance it will be easy to change background of the scrollBar.
APPROACH 2:
You can post notifications whenever you want to change the scrollBar color,
add observer for the notification you are posting and with the notification you can pass the color you want to be set for all scrollBar.
Posting Notification when you want to change color
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:#"someKey"];
[[NSNotificationCenter defaultCenter] postNotificationName: #"TestNotification" object:nil userInfo:userInfo];
Adding Observer in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveTestNotification:)
name:#"TestNotification"
object:nil];
Method to handle received Notification
- (void) receiveTestNotification:(NSNotification *) notification{
NSDictionary *userInfo = notification.userInfo;
UIColor *backGroundColor = [userInfo objectForKey:#"someKey"];
// assign this color to your scrollBar in each ViewController
}
REMOVING OBSERVER in viewDidUnload
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"TestNotification" object:nil];

Notify UI update in AppDelegate (UITabBarController badge)

In my app, UITabBarController is an initial ViewController build in storyBoard. I would like to observe data updating in my whole app. So I add an observer in appDelegate...
AppDelegate.m
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(shipCountBadge:) name:kShipCountBadge object:nil];
- (void)shipCountBadge:(NSNotification *)notification{
Groups *vc = [[Groups alloc] init];
[vc addBadgeCount:notification.object];
}
Group.m
- (void)addBadgeCount:(NSNumber *)count{
NSLog(#"d",[count intValue]);
[[self.tabBarController.tabBar.items objectAtIndex:2] setBadgeValue:[NSString stringWithFormat:#"%d",[count intValue]]];
}
Its doesn't change the UI at all. I would like to know how to update UI in AppDelegate actually? Anyone has an idea :)?

NSNotification delay from class A to class B

My NSNotification is delayed.
In myVC, I have two buttons, buttonA and buttonB. Each one is linked to their respective pdfs, pdfA and pdfB. There is a button Push, which is pressed after A or B is pressed. Push brings the user to the RVC where there is a UIWebView.
I want it so that by pushing either A or B, the UIWebView will display the respective pdf file. To debug this, I set it so that instead of changing the pdf, it will display text using NSLog. However, it doesn't work until after go back to myVC from the RVC by pressing a different button.
in myVC.m file:
- (IBAction)open_pictures_A:(id)sender
{
//do some button alert popup action/whatever button does
RootViewController *dataObject = [RootViewController new];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:dataObject
forKey:#"buttonA"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification"
object:nil
userInfo:userInfo];
}
And there is one like that for buttonB, but forKey would be "buttonB"
in the viewDidLoad for myVC.m I have
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification"
object:nil];
in my RVC,
RVC.h I have
#property (nonatomic, strong) NSString *property1;
And in the RVC.m I have
- (void)viewDidLoad
{
// other stuff
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(eventListenerDidReceiveNotification:)
name:#"MyNotification"
object:nil];
}
- (void)eventListenerDidReceiveNotification:(NSNotification *)notif
{
if ([[notif name] isEqualToString:#"MyNotification"])
{
NSLog(#"Successfully received the notification from buttonB!");
NSDictionary *userInfo = notif.userInfo;
RootViewController *dataObject = [userInfo objectForKey:#"buttonB"];
// Your response to the notification should be placed here
NSLog(#"dataObject.property1 -> %#", dataObject.property1);
}
}
However, the log entry only shows up when I press a button to exit out of the RVC back to myVC
Here is the code I use to go from mVC to RVC
-(IBAction)goToRVC{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
RootViewController *RVC = [storyboard instantiateViewControllerWithIdentifier:#"Root"];
[UIApplication sharedApplication].delegate.window.RootViewController = RVC;
}
Then from RVC to myVC
-(IBAction)backtomyVC{
[[[UIApplication sharedApplication] delegate] performSelector:#selector(myVC)];
[self disconnect];
}
Why is my notification action being delayed?
It's not because of delay.
When you press your button A or B you create new RVC object and you post the notification however you don't present/push RVC view controller (you just initialise it) so the RVC hasn't fired viewDidLoad method and it hasn't register itself as an observer.
After that you call goToRVC method where you create RVC object and you add it to the view hierarchy so the viewDidLoad method is call and the object register itself as an observer.
When you go back to mVC the RVC is not deallocated yet so it receive the notification and you can see the log.
Hope it's clear.

Presenting a View Controller with an xib from an SKScene

I have tried many things, none of which are working. I have an SKScene, and when the game is over, I want it to go back to the original view controller. Thank you in advance.
I have tried this in my SKScene
homeScreenViewController *viewCont = [[homeScreenViewController alloc] init];
[viewCont viewDidLoad];
This in my other view controller
constructinoViewController *view = [[constructinoViewController alloc ] init];
[self presentViewController:view animated:YES completion:nil];
It mostly says The view is not in the view hierarchy.
You need a way to send the parent viewController a message.
In your viewController which presents the SKScene, add the following line in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(closeScene) name:#"closeScene" object:Nil];
Also, add this method
-(void)closeScene
{
//Remove the SKView, or present another viewController here.
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"closeScene" object:nil];
}
Then in your SKScene, at the point where your game ends,
[[NSNotificationCenter defaultCenter] postNotificationName:#"closeScene" object:nil];
You can do this by using delegates as well.
If you have presented the ViewController, then you need to dismiss it to return to previous ViewController.
You should use
[self dismissViewControllerAnimated:YES completion:NULL];

Update splitViewController from ModalViewController

I need to update on of my splitView's from a modalView, but I am not having any luck.
for instance: From my rootView (which is a UITableVIewController) if I set an option to hide a certain section from the rootView table in my ModalView, when I dismiss the modalview, the setting doesnt take affect on screen for the tableview, same goes for detailView.
i've tried:
MyRootView *mrv = [MyRootView alloc] init];
[mrv updateTable];
[mrv release];
[mrv updateTable]; is located in my RootView and contains a [tableView reloadData];
If I place an NSLog in there, that prints, just that the table doesnt reload while on screen.
Even tried viewWill/DidAppear, no avail.
Any help much appreciated!
So I was able to resolve this issue with Notifications.
[[NSNotificationCenter defaultCenter] postNotificationName:#"update" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateView:) name:#"update" object:nil];
- (void)updateView:(NSNotification *)notification {
[tableView reloadData];
}

Resources