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];
Related
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 )
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 :)?
Here's the question:
Can one View Controller add another View Controller as an Observer to the defaultCenter before the second view has been loaded?
I have a model class that creates a NSURLSession, grabs some data, builds an array, and sends notifications that it's done (along with a pointer to the array).
My app loads with a Map View that instantiates the model, calls the method to create the array, listens for the notification, and drops pins using the array.
I have a Table View tab that I want to load using the array built by the map.
Can my Map View add my Table View Controller as an observer before the Table View is loaded?
Something like:
[[NSNotificationCenter defaultCenter] addObserver: TableViewController ...
Thanks for any insight. I'm figuring this out as I go.
-----------------EDIT--------------------
viewDidLoad from MapViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
_mapView.delegate = self;
_model = [[WikiModel alloc] init];
_lastArticleUpdateLocation = [[CLLocation alloc] initWithLatitude:0 longitude:0];
_lastUpdateUserLocation = [[CLLocation alloc] initWithLatitude:0 longitude:0];
// Listen for WikiModel to release updates.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadData:)
name:#"Array Complete"
object:_model];
//table view listener attempt ...
UITableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:#"tableViewController"];
[[NSNotificationCenter defaultCenter] addObserver:tvc
selector: #selector(updateDataSource:)
name:#"Array Complete"
object:nil];
[self.navigationController pushViewController:tvc animated:YES];
}
From the TableViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Listen for WikiModel to release updates.
/*
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateDataSource:)
name:#"Array Complete"
object:nil];*/
}
-(void)updateDataSource:(NSNotification *)notification
{
_wikiEntries = [[notification userInfo] objectForKey:#"wikiEntryArray"];
[self.tableView reloadData];
NSLog(#"************received***********");
}
This is possible as long as you pass the pointer to your view controller as the observer. Here's an example:
//Go to the detail view
VC2ViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"VC2ViewController"]; //ID specified in the storyboard
[[NSNotificationCenter defaultCenter] addObserver:dvc selector:#selector(notificationReceived:) name:#"MyNotification" object:nil];
[self.navigationController pushViewController:dvc animated:YES];
Then, you can post your notification as usual:
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification" object:nil];
I just tested the above code and it worked fine for me, the following function was called in my detail view controller:
-(void)notificationReceived:(NSNotification *)notification {
NSLog(#"RECEIVED");
}
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.
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];