Is there a way when using Storyboards to identify when a segue is called for the first time. I am using prepareForSegue:sender: to inject a pointer to my model into the segue destination controller, but I only want to do this when the controller is first created.
Currently I am checking the dataModel pointer in the destination controller and only setting it if it returns null. This works just fine, but I wanted to check I was not missing another way of accomplishing this?
there will be another solution by using your appdelegate
in the function :didfinishlaunching you can create a bool for the app and then check it if it is already called or not
hope this also helps..
Related
I'm trying to learn how to pass data between views. Say set a label in the second view from text entered into a text field on the first view. I basically have tried making a string in the second view and then when switching from the first view to the second I set a string in the second view. Then when the second view loads its sets the text of a label to the same string. I NSLog right before and after the transition, before its fine, but when the second view loads it string gets erased. I'm not sure why this isn't working. Here is my project: http://www.mediafire.com/?83s88z5d06hhqb5
Thanks!
-Shredder2794
From my book (http://www.apeth.com/iOSBook/ch19.html#_storyboards):
Before a segue is performed, the source view controller is sent prepareForSegue:sender:. The view controller can work out what segue is being triggered by examining the segue’s identifier and destinationViewController properties, and the sender is the interface object that was tapped to trigger to the segue (or, if performSegueWithIdentifier:sender: was called in code, whatever object was supplied as the sender: argument). This is the moment when the source view controller and the destination view controller meet; the source view controller can thus perform configurations on the destination view controller, hand it data, and so forth.
(Of course another solution is "don't use a storyboard". Then the first view controller creates the second and can hand it data then and there.)
The reverse problem is much trickier; look at the Utility Application template for an example of how to use the delegate pattern.
StoryBoards are ready made things where you can reduce a lot of code you write.So consider controller A & B on storyboard.
Now for passing data From A to B you can connect them with a segue name its identifier and then you can use delegate methods in A as:
//This method gets called before transition from A to B.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"THE IDENTIFIER YOU NAMED"])
{
id *objectOfController_B = [segue destinationViewController];.
objectOfController_B.lblTextDisplayOfA = //Something...
}
}
Now You can Explicitly transition it by using button in controller A.
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"THE IDENTIFIER YOU NAMED" sender:sender];
}
So I guess you can try experimenting on this and you will get it how and when transition occurs with segue.
Hope this helps.
There appear to be more than a few things to explain. I think working through a few tutorials will give you the answers you need. See http://www.raywenderlich.com/tutorials
i've asked more or less the same question a few weeks/month ago. there were some very good answers, especially the one from zoul, who built a demo project that will show you how to create a factory pattern application that will provide the views with the needed objects.
my question can be found here: iOS: Initialise object at start of application for all controllers to use and have a look at the answer from 'zoul'. it got me through this problem =)
good luck trying it out =)
sebastian
I spent "countless hours" my self trying to find a way to pass data and understand delegates with no comprehension and very little success. This video did something that all the other references I checked didn't do : keep it as simple as possible while clearly showing what was needed. Thank you so very much Mr Rob Smythe. http://www.youtube.com/watch?v=XZWT0IV8FrI
Have a look at this: http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html
Delegate pattern is a common way to achieve what you are trying to do.
I am building a login onto an completed app. The app have already used the appdelegate but in order to do the login, it needs to use the appdelegate. I am stuck at a point where I cannot "make a new reference outlet for your Navigation Controller to your App Delegate." since the appdelegate in not in my .xib.
How can I make the appdelegate appear on the .xib file so I can link it with the navigation controller?
Thanks.
We need to first understand the architecture of your app. But even without that, one solution that I could think of is this.
Create a viewController named ValidateViewController by right clicking on your project-> add new file -> UIViewControllerSubClass and check the xib option as well.
This will generate the following
ValidateViewController.h
ValidateViewController.m
ValidateViewController.xib
Write all your validations functions in this class. Write a function in this class that would return true on validation success and false on validation failure. Now let's use this class in your appdelegate.
Now in your appdelegate.h, import this ValidateViewController.h. All the validation functions that you defined in the ValidateViewController will now be exposed to be used just by creating and allocating an object of ValidateViewController in your appdelegate. I hope this is pretty straight forward for you. If not, we can look at it again.
Now in the applicationDidFinishLaunching method of your appdelegate, the first thing you do is you load this ValidateViewController as a modalView controller programmatically. Once loaded, call the functions and get the return values from your validate functions, if validation succeeds, dismiss this modalViewController otherwise, you pop up an alertView in the modalViewController saying, validation was unsuccessful and the user stays on the ValidationViewController. I think that should solve your problem. If you need more help please come back. If you find the answer satisfactory, please accept it.
I've recently started developing for the iPhone and so far I'm doing pretty good but there's this basic pattern I really don't seem to get.
Say, I have a TabBar with two views and a custom delegate protocol, thus my structure is the following:
AppDelegate.h/.m
myDelegateProtocol.h
FirstViewController.h/.m
SecondViewController.h/.m
MainView.xib
FirstView.xib
SecondView.xib
Now I want to achieve the following: I placed a button in the FirstView.xib and I'd like the IBAction which it invokes (inside FirstViewController ofc.) to send a message to the SecondViewController ([self.delegate tellSecondViewContrToSayHi]) and invoke another method which simply prints a log into the console saying "hi I'm here."
So far I know what I need to do in theory:
Specify the protocol.
Implement the protocol in the SecondViewController.
Create an id< myDelegateProtocol > delegate inside my FirstViewController,...AND last but not least:
Set the self.delegate = secondViewControllerObject.
Now, nr.4 is where the problem's at. How on earth do I link the delegate to the other viewController? I mean I'm not the one instantiating the views as the tabBar kinda does that for me,... any advise? Or am I just way too tired to notice a really stupid thing I did somewhere?
Theoretically the same question also applies to the target:action: thing,... I mean, how do I define the target?
Thanks a lot,
wasabi
You have the right idea, assuming that you want relatively tight coupling between these controllers via that delegate protocol.
Since neither controller knows about the other until that delegate property is set you need to have some object which has a reference to both of them wire up that relationship. In your case that's probably the application delegate which can create both controllers, set one as the delegate of the other, and pass both along to your tab bar controller.
What you might actually want is to have the app delegate give both controllers a reference to some shared model object. Your FirstViewController can update that model when you tap a button and your SecondViewController can observe changes to the model to update it's display (or just update its view when it appears based on the current model state). That way your controllers don't need to know anything about each other.
I have a app that uses several view controllers. In one instance I need to uses an int (int lives) in a seperate view controller from where it is created. I have tried using it and it throws and error at build claiming "lives" undeclared. I am already importing the view controller where the int was declared. I am stuck on this one.
I would appreciate any help.
Use properties, or custom setter and getter for instanse variables.
You could use the application delegate to store information you need in different places in you app.
The proper way would be to implement your own delegate protocol. For a recent discussion on this topic, see pass a variable up the navigation stack
This may be very basic, but I just can`t figure out what to do, so thanks for any response...
I`m using a navigationcontroller and are currently on the second level in the stack. Here i set a string value and use popViewControllerAnimated to go back to first level in the stack.
What might be the best solution to use that string value from second level in the stack? I`ve tried to set a value in the first level manually in the second level, but I must be doing something wrong...
Thanks!
edit: Im very new to both objective-c and C in general so im still a bit confused :(
Consider applying MVC pattern in your program - store the string value in a separate globally accessible storage class (in simple cases you can use application delegate or create a singleton object for this purpose). Then in your 2nd level controller you set the value in the storage and in 1st level you get it from the storage.
Would it not be better to use a delegate pattern? You could define your top-level view as a delegate of the second-level view; you set the delegate property of the second-level controller before pushing it on the stack.
Once the string has been selected, before popping the second-level controller, call the delegate method with the string as its argument.
The advantage of that would be that it'd still work even if you have multiple instances of the same 2nd level controller (eg in a tabbed interface).
It's not the best solution, but it works:
[[self.navigationController.viewControllers objectAtIndex:0] setSmt:#"123"];
[self.navigationController popViewControllerAnimated:YES];
The value "smt" in the parent view controller will change.