Using object from one class in another - iphone

I have a two UIViewControllers, on one of which I want a custom button to be displayed. I have written the code for this. I have also written the code for a popover segue to the other ViewController, and as both controllers belong to my 'ViewController' class, the button is also displayed on the second ViewController, which I do not want.
Obviously I can create another class and make my second ViewController a member of this class, however I want the code for my second ViewController to change the colour of the button in the first ViewController, so how can I use an object from my first class in the second class implementation file?

I have a two UIViewControllers, on one of which I want a custom button
to be displayed. I have written the code for this. I have also written
the code for a popover segue to the other ViewController, and as both
controllers are subclasses of my 'ViewController' class, the button is
also displayed on the second ViewController, which I do not want.
Why don't you make few property private in first cass so that it doesn't get inherited.
Obviously I can create another class and make my second ViewController
a member of this class, however I want the code for my second
ViewController to change the colour of the button in the first
ViewController, so how can I use an object from my first class in the
second class implementation file?
Your main problem is to access object of ClassA from ClassB.
Here are a lot of ways, and answers here, to do :
Update a label through button from different view
Xcode - update ViewController label text from different view
Setting label text in another class
Set label on another view to stored NSDate

Related

iOS & Swift: ViewController class is defined, but when is it created as an object?

Learning Swift as my first new language in many years, I've come across something I'm curious about using Xcode and creating a new iOS project using the single view template.
In the default ViewController.swift file, UIkit is imported, and then the class ViewController is defined, inheriting from UIViewController. But I can't seem to find out where or how this class is ever created or initialized as an object.
Default ViewController.swift example
If I define my own class, or even want to create another view controller, I must first initialize it as an object somewhere in order to use it. So where is this default ViewController getting made?
Thanks for any help you can offer for me to try to conceptualize this!
Open your "Main.storyboard" file. Make sure View Controller is selected on the left sidebar:
Then on the right sidebar you can see that your ViewController class is in the class field:
So, when your app is loaded, the default storyboard is loaded, and that storyboard is responsible for creating an instance of the ViewController class and set it up for you. To test this out, you could create a new class, say ViewController2 and make it inherit from UIViewController. ViewController2 would then be available in the right sidebar:
And then your ViewController2 code will be used instead of ViewController.
You have changed the location of the ViewControllerQuestion.app file
I believe that the UIApplicationDelegate takes care of the creation of the initial view controller. You still can manually create view controllers as you mentioned and segue to them.
Normally you can create a view controller in a storyboard and use the Interface Builder to set up a segue to that view controller.
I hope this answers your question.

How to set a view's outlets' values from within another class

In xcode, If we have a view controller that has some subviews' (eg: label) outlets, and we want to change its text from the AppDelegate.swift class whenever an app is becoming active after suspended, How can this be done? I have tried making an instance of this view controller class from the AppDelegate class, but that didn't work because it made another view instance instead of the view itself, and we can't set the outlet as static. I have also thought of making a static ViewController property observer, but since it is static I still can't set the outlet from inside it. So, how can we do such a thing?
The best way to achieve what you want - is notifications. In your view controller add observer which will change your label's text or whatever. From AppDelegate method post notification. If you need an example, I can help you.
If we have a view controller that has some subviews' (eg: label) outlets, and we want to change its text from the AppDelegate.swift class whenever an app is becoming active after suspended, How can this be done?
Don't. Never speak directly to another class's outlets. Give that class a method that you can call where that class will respond to the calling of that method by speaking to its own outlet.
I have tried making an instance of this view controller class from the AppDelegate class, but that didn't work because it made another view instance
Correct. You need to get a reference to the existing instance of the other class. To do that, you need to know your own view controller hierarchy / structure or else provide one class with a delegate / reference to the other class at the time of creation.

How to make a combo box in iphone/ipad?

This is maybe a simple question, but I don't have a clue the keyword i had to search for this...
I want to create a simple selector (I called it spinner in Android). This is what i want to achieve
Or it will take a whole screen if it's in ipod touch / iphone.
So, I have 3 button that represent filter (category, country, sort) for a ListView... and if I press one of the button, a popover / dialog should be appear to select the filter for each button.
thanks...
Please let me know if I need to add some information to make the question clear.
Its not so much typical that is it appearing.
Create a new UIViewController class with xib and adjust view size you want to display for combo box or popviewControoler. Then put navigation controller over and tableviewController.
And customize your UIViewController using its controller class. Controller class will be reponsible for displaying and selecting data.
Now in your MainViewController From where you want to show ComboBox or popviewcontroller.
declare popViewController instance variable,synthesize it.
implement a userdefined method here alloc your popviewcontroller class and assign it to popviewController instance variable of your class.
Then called it didSelectRowAtIndexPath.
When popover dismiss you set popViewController result in instance variable of this class so it can easily access in MainViewController class.
You can achieve combo box like functionality by adding a UItableView in a UIView and implement all the delegates of UITableView in that custom view.
Now you can add object of that custom UIView where ever you want that combo. Just you have to workout with some Frame setting.

iOS5 Second View Controller

I'm trying to create a second view controller and link it to a new set of .m & .h files then alter the text of a label on that view controller under the viewDidLoad but am having some issues.
Here's what I am doing so far.
I'm starting with a blank new single page, from the MainStoryboard, I add a new VC and add buttons so I can navigate between the pages. This works fine.
Next I create a new Objective-C Class, "SecondViewController" and choose subclass UIViewController.
Now back on the Storyboard I select the other ViewController and try to pick the new SecondViewController as a Custom Class but it doesn't show up in the list of available classes.
Now, if in step 2 I choose the UIView subclass I can assign it to the ViewController in step 3 but the viewDidLoad options are not available so I am not sure how to program changes to the VC.
What am I missing here?
I am able to use the UIView and use the following to make the change I am asking about. I am not sure if this is the correct way to do this.
- (void)drawRect:(CGRect)rect
{
myLabel.text = #"change2";
}
EDIT ----
Okay, I've figured it out. I was clicking on the VC's View (background) in the Storyboard and not the actual ViewController! Now it works the way I thought it should. I suppose if I had looked at the View Controller Scene I would have noticed what I was doing incorrectly.
Now, if in step 2 I choose the UIView subclass I can assign it to the
ViewController in step 3 but the viewDidLoad options are not available
so I am not sure how to program changes to the VC.
The class of your ViewController (or similar one) from interface builder and another class in your code must extend the same class

Finding the calling class of UIBUTTON?

I have a UIViewController, which is used in 2 different classes within same xib. I have created a uibutton in it and associated the action method in both the classes which are trying to use it.Both classes A and B have different outlets to that UIVIewController.
when i try to press the UIButton in the viewcontroller, both functions in Class (A) and Class (B) get called (since the event is on touch down) but the method names in each class is different.
Can I somehow check which class called the uiviewcontroller's button touch event?
Set a tag(different values) for Button from each class and in your button function first check the tag and execute respective action.
you can check with isKindOfClass if you have 2 classes