My issue for my function is I am not sure how to pass the data which i selected, and back to the previous view controller. The link is to the whole paragraph,http://pastebin.com/AtMjLD66 (sorry I don't have 10 reputations, sorry about the inconvenience ) Cheer.
I would recommend delegates for this but if you don't want to do so, NSNotification would save your day here.
Add this is in advanceVC.m probably in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(someThingSelectedInDetail:) name:#"someThingSelectedInDetail" object:nil];
- (void)someThingSelectedInDetail:(NSNotification*)notificationObject
{
NSString *chefName = notificationObject.object;
NSLog(#"Chef Name from notification: %#",chefName);
}
And in didSelect of advanceDetailVC.m do this.
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[[NSNotificationCenter defaultCenter ]postNotificationName:#"someThingSelectedInDetail" object: cell.textLabel.text];
Instead of passing a label's text you have the indexpath here so take it directly from the array and pass it in the notification like this
[[NSNotificationCenter defaultCenter ]postNotificationName:#"someThingSelectedInDetail" object: [detailArray objectAtIndex:indexPath.row]];
You would use the delegate pattern to send a message back to the view controller's delegate (which you could set to the presenting view controller), passing along whatever information you need to about the selection.
Assuming you want it for iOS, a simple call :
[[[UIApplication sharedApplication] delegate] doSomething];
or
Since you only have one view controller, the generic way (independent of how your app was set up):
UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
You can use nsuserdefault to save data and then you can retrieve back in the previous class
Related
I have two classes I would like them to speak with each other. Class A contains a tableView and when users hitting a table row, I fire my didSelectRowAtIndexPath method. In this method I need to inform class B about this through a delegate. I know how delegates work but having a hard time to figure how to set the delegate of A without using the prepareForSegue method.
Normally I would do this when I set up my delegate
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"goToManipulator"]) {
ManipulatorViewController *secondVC = (ManipulatorViewController *) segue.destinationViewController;
[secondVC setDelegate:self];
}
}
But how can I set the delegate without the use of prepareForSegue?
Thanks in advance
EDIT :
This is how the structure of my storyboard looks like. The "receiver" viewcontroller is the one that will get the data and display in the "current name" label depending on what's been selected in the tableview from the "sender" viewcontroller, closest to the right.
http://oi62.tinypic.com/2li99w1.jpg
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ManipulatorViewController *secondVC = [[ManipulatorViewController alloc] init...];
[secondVC setDelegate:self];
//if you use push transition in UINavigationController
[self.navigationController pushViewController:secondVC animated:YES];
//if you use modal transition
[self presentViewController:secondVC animated:YES completion:nil]
}
init... means that initialization depends on your program architecture.
EDIT
If you want to get secondVC from storyboard, use
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ManipulatorViewController* secondVC = [storyboard instantiateViewControllerWithIdentifier:#"secondVC"];
And don't forget to add identifier for your viewController in storyboard.
I understand your use case like this:
In the Receiver, you open the Sender. There you select a value, and after selecting the value you want to tell the Receiver about the new value.
You can create a protocol on Sender, that Receiver implements. Then, in the function that catches the chosen value in Sender, you call the protocol method (e.g. didSelectNewName() or something).
Of course, you need a handle to the Receiver, which you typically get via the delegate. But wether you use a segue or other method to transition from Receiver to Sender, you will all the same have the opportunity to set the delegate of the Sender.
If this is not what you are looking for, please explain exactly how you initialize the Sender, and why segue is not desirable.
Is View Controller B already instantiated when A's cells are tapped? If it is and you're not using prepareForSegue to get the other View Controller's identity, it might be better to use NSNotification Center. In View Controller A's didSelectRowAtIndex method, you can put
[[NSNotificationCenter defaultCenter] postNotificationName:#"yourNotificationName" object:nil userInfo:dictionaryWithYourData];
and it will put up a notification to your whole app that the row was selected. If you initialize a dictionary with any info you want before hand, it can be passed through userInfo. Then, in View Controller B's viewDidLoad, add
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(yourMethod:) name:#"yourNotificationName" object:nil];
to make it listen for the notification. The selector you set will accept the NSNotification as a parameter, so you can get the dictionary as follows:
- (void)yourMethod:(NSNotification *)notification
{
NSDictionary *yourData = [notification userInfo];
}
Here's what I do.
In the .m file:
#implementation ViewController{
SecondViewController *svc;
}
And then below you need to an action like this:
- (IBAction)goToView2:(id)sender {
if (!svc) {
svc = [[self storyboard] instantiateViewControllerWithIdentifier:#"View2"];
[svc setDelegate:self];
}
[[self navigationController] pushViewController:svc animated:YES];
}
Just make sure to set the correct identifier in the StoryBoard to the ViewController where protocol is declared.
I have two view controllers, FirstViewController and FourthViewController. FirstViewController is my initial view controller. I present FourthViewController with
UIViewController *fourthController = [self.storyboard instantiateViewControllerWithID:#"Fourth"];
[self presentViewController:fourthController animated:YES completion:nil];
Then, in FourthViewController's .m I'd like to change the text of a UILabel in FirstViewController. So I use
UIViewController *firstController = [self.storyboard instantiateViewControllerWithID:#"First"];
firstController.mainLab.text = [NSMutableString stringWithFormat:#"New Text"];
However, after I use
[self dismissViewControllerAnimated:YES completion:nil];
I find that my mainLab's text has not updated. Does anyone know why?
When you are calling this line from FourthViewController.m you are actually creating a new instance of FirstViewController, rather than using the already created one.
UIViewController *firstController = [self.storyboard
instantiateViewControllerWithID:#"First"];
You can tackle this in two ways.
1) Using notification
post a notification from FourthViewController when label text need to be changed.
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateLabel"
object:self];
In your FirstViewController viewDidLoad methodcreate an observer that waits for this notification to get fired.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateLabelCalled:)
name:#"updateLabel"
object:nil];
Implement updateLabelCalled: and update label.
- (void) updateLabelCalled:(NSNotification *) notification
{
if ([[notification name] isEqualToString:#"updateLabel"]){
//write code to update label
}
}
2) Implementing delegate
It is already explained here in stackoverflow. The basic idea is you create a FourthViewController delegate, and create a delegate method to updateLabel. FirstViewController should implement this method.
If you want to update the label on first screen and nothing else then go for notifications. It's better rather you write the delegate. Because you want to update only label text thats it.
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 have an app that has a navigation controller in a tabbar controller. The root for the navigation controller is a table view controller. The table view has a segue to an image view. I want to update the badge value on a tab from the image view controller. From the tabbar controller, this works fine:
UIViewController *viewController = [self.tabBarController.viewControllers objectAtIndex:1];
viewController.tabBarItem.badgeValue = #"x";
But when I put the same code into the imageview controller, it doesn't work. When I check the value of 'viewController' after it executes, the value is nil. Same for self.tabBarController. For some reason the image view controller can't see its tabbarcontroller.
You should be able to access the tabBarController with self.navigationController.tabBarController. So your code needs to be:
UIViewController *viewController = [self.navigationController.tabBarController.viewControllers objectAtIndex:1];
viewController.tabBarItem.badgeValue = #"x";
so there are many ways to do this but the best way would be subcscribe to an NSNotification and then from your image view or wherever, make that notification so it would look something like.
[[NSNotificationCenter defaultCenter]
addObserver:objectToListen
selector:#selector(methodToRun:)
name:#"NotificationName"
object:nil];
And then in you deeper items such as the imageview or whatever you post it like this
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificationName" object:nil userInfo:dictionaryWithValuesForMethodToUse to use];
And then you get the information from the userInfo like so in you method that changes the badge value
- (void)methodToRun:(NSNotification *)notification
{
NSDictionary *dictionary = [notification userInfo];
}
I have created a Tab Bar Application using Xcode that as two views.
The secound view is a UITableViewController.
What I am struggling to do is send data to this view, when the second tab is pressed. I have delegated the Tab Bar to my AppDelegate class and implemented this function:
-(void)tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
{
// Override point for customization after application launch.
statisticsViewController* assignmentListcont = [statisticsViewController alloc];
NSManagedObjectContext* context = [self managedObjectContext];
assignmentListcont.managedObjectContext = context;
[assignmentListcont release];
}
The second view is displaying fine but the data hasn't been passed. I imagine its because I haven't programmed the second views transition but I'm unsure of how to do this if I already have a .xib file doing it for me? Is there some way to just pass the data without problems or even retrieve the data once inside the view?
You could use notifications.
In the view that you want to receive the data, put this in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(yourSelector:)
name:#"addedData"
object:nil];
Then implement the method that receives that data:
- (void)yourSelector:(NSNotification *)notification {
Foo *foo;
foo = [notification object];
//do something else
}
Now in the class where the data is originated from, you post a notification that new data was created. Also, you pass along the data that you want to have the other method receive.
[[NSNotificationCenter defaultCenter] postNotificationName:#"addedData"
object:foo];
I've done this to send a value from one view controller to another, hope it helps
UITabBarViewController *var = [self.storyboard instantiateViewControllerWithIdentifier:#"name"]; //I select the UITabBarController
otherViewController *var2 = [var.childViewControllers objectAtIndex:0]; //I Select the first ViewController from that UITabBarController
var2.variable = #"value";
[self.navigationController pushViewController:var animated:YES];