Get an event from an other view controller - objective-c

I have two ViewController. The first contains and displays an array with values.
The second is a picker view (modal view controller) that permits to choose a columns to add on the array (with a button "ADD").
But, I don't know how retrieve an event when user click on "ADD" button to refresh my array because IBaction function and the array are not in the same controller.
Thanks for your help.

Protocols can be helpful for such situations.
A protocol is simply a list of method declarations, unattached to a class definition.
Protocols can be helpful in a number of scenarios, a common usage is to define methods that are to be implemented by other classes. A familiar example is when using a tableview, your class implements the cellForRowAtIndexPath method which asks for cell content to insert into a table – the cellForRowAtIndexPath method is defined within the UITableViewDataSource protocol.
Simple objective-c protocol example

You can also pass one local Variable when You press your add button and after that you reload your array in table view.
I think you want same like add contact add field function in simulaor.
Welcome.

I consider that,you have an array in first view controller that you want to access in second view controller on button event (IBAction):
Create Method in secondviewcontroller with array as parameter like this.
-(void)methodname:(nsmutablearray *)array;
Call the above method in first view controller and pass your array in this method when you navigate to second view controller by initializing it.
So, In second view controller you will get an filled array that you can use further.

Related

Cocoa binding search field to tableview in a different nib

I've got a user interface that looks pretty much like iTunes. For purposes of encapsulation, the top area and the main table view are in separate classes with separate nibs. I want to bind the search field from the top view controller to the tableview in the bottom view controller. I've arranged it so there are properties to store the NSArrayController in both classes. The array controller is an array of dictionaries, and the dictionaries have a "search_keywords" key that I want to use to filter the tableview.
Is it possible to set up the search stuff in Interface Builder even though it's in a separate nib? I can't figure out what to put in the various boxes.
If it's not possible with IB, I assume it's possible in code, since there is a view controller with references to both of the sub-view controllers and I can get at the search field, table view and array controller objects through properties on the two classes.
How do I set it up? IB would be best, if it's possible.
What I did was use the NSSearchField in the top view controller as a dummy/placeholder. I create the "real" search field in the tableview's nib, and wire up all the bindings stuff as per normal. Then in the main view controller I grab the search field out of the tableview's nib, and replace the dummy searchfield with the real one using replaceSubview:with:
Now I can continue to use IB to modify the bindings, and it doesn't really matter what's in what nib as it all gets placed properly in the view hierarchy at runtime.
In your concept you cannot bind the search field in IB in the desired way.
Do it like
1. Create an accessor-method (accessorMethodForTextInSearchField or what name you want to use) in the TopClass for the Text in the searchField
2. Import the TopClass.h in the MainClass
3. In MainClass you can use
NSString *searchString = [ NSString stringWithString:[ TopClass accessorMethodForTextInSearchField] ];
4. Now search for searchString in the array

How can I use a variable in two modal separated view's with the same class (.h and .m files)?

I am trying to use a variable (birthDateLabel) in two view controllers that are separated via a modal but use the same view controller class (CreateAccountViewController). The label works in the view that it is set in but after [self dismissViewControllerAnimated:YES completion:nil]; is run the varriable is reset, how can I retain it?
Order of operations:
The first instance of CreateAccountViewController is run
A button is tapped to go to a new instance of CreateAccountViewController but this instance has a different view with a UIDatePicker to set the birthDateLabel variable.
The birthDateLabel is set
The user taps done and dismissViewControllerAnimated is run
The app updates a UILabel on the first instance of CreateAccountViewController
Step 5 is what does not work, if I put the label on the view as the picker it works but when the modal is dismissed the variable is reset. How can I keep the variable set after the modal is dismissed? Or is my only option to create separate view controller classes?
I tried to do my best explaining this, but if you need me to explain it more just comment.
The standard way to do this is to use a delegate. The fact that your two controllers are both instances of the same class doesn't make any difference, other than they will both have a variable called birthDataLabel. The value of that variable is specific to each instance, just as it would be if these were of two different classes.
So, you should do it the right way, which is to create a delegate protocol in your second instance, and have the first one set itself as the delegate when it first presents the second one.
#rdelmar provides the most technically correct answer, but if it's just one value you could simply use nsuserdefaults. If the number begins to expand creating a delegate might be a smart choice.

loading different text in same view controller

Hi I have a quiz app that I am working on, and on my RootViewController I have two buttons with action methods, now when I press on one of the buttons I get my questionViewController and text is loaded into specific fields, I do this by using the viewDidLoad method. When I go back to the RootViewController and click on a different button I want to show the same questionViewController but with different text, how can I go about doing this?
I just finished a project with similar requirement. One way to do this is to use static variable and class method in your questionViewController class.
You could implement viewDidAppear: on your questionViewController, and set the text there instead of viewDidLoad.
You could also implement a method on questionViewController, that updated the text fields, and call this method from the RootViewController.

Passing values when popping a view controller with a UINavigationController

I have a pretty basic question. Assuming I have a UINavigationController set up for my app and I want to pass a value to the previous view when the user hits the "back" button in the UINavigationBar. I know I call popViewController:, but is there a way to pass a value back with it? Assuming there is (which there must be), what's the best way to do this?
Thanks!
What you should do in this case is think about the problem in a different way. You can create a delegate protocol on the view controller you want to pass information back from. This protocol can have any number of methods, at least one of which will look something like viewController:(MyViewController *)theViewController didSelectThisInformation:(NSString *)someInformation. When some action takes place on this view controller, it makes a call to its delegate (which conforms to the protocol you defined) and calls the appropriate method. When pushing this view controller, you would set its delegate property to the object you want to receive that information.

How to access methods/variables from swapped custom view

I've created a window that contains an NSSplitView in which case the right custom view has a view that I swap into at runtime. The custom view swapped in contains a NSTableView with data inside it. I have a search box in the main window of the application that I want to be able to constrain the rows of the table view with.
I have the code to do this and I know it works, but the code I have was tested with a search text box and table view that were on the same window scope. With the text search box now being in the main window and the table view being in a different custom view, I'm not sure how to get the text search box to call the relevant methods from the custom view's controller class, because I don't have direct access to these method anymore.
I'm sure this is a very beginner question, but any help would be appreciated. Thanks.
Have your main window controller pass the search query or filter predicate to a property of the content view controller.
You can give the main window controller a weak-referencing (assign) property that holds the current content view controller. Implement a custom setter that not only assigns to the backing instance variable, but also does the swap. That, any time it's time to do a swap, you simply say self.currentContentViewController = viewControllerToSwapIn, and when it's time to change the query/predicate, you pass it to self.currentContentViewController.searchQuery (having implemented the searchQuery property in the MainContentViewController class and made all your actual content-view controllers inherit from that class).