Finding the calling class of UIBUTTON? - iphone

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

Related

Any differences between #objc and IBAction when programmatically creating UIButton event?

When creating an eventhandler from a UIButton from the Storyboard, Swift adds #IBAction before func. When adding an event programmatically to a UIButton to a func, Swift gives a error and says I need to add #objc in front of my method, but when I add #IBAction, it compiles as well.
Are there an difference between the 2 and which should I use when adding an event to my UIButton programmatically?
The IBAction prefix already exposes the method to the Objective-C runtime, so there is no need to prefix it additionally with #objc.
Since IB stands for Interface Builder, it's unnecessary to use it when you create a button programmatically. All it does (besides exposing the method to Objective-C) is make Interface Builder list the function as an action in the Connections inspector.

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.

Create an outlet in storyboard to an inherited property

I'd like to create a basic view controller class BasicController with two subclasses SubclassController and AnotherSUbClassController. BasicController controls view that have a button inside them, so there should be a
weak var buttonThing: UIButton?
property in it. In the actual implementation I want to use the subclasses in various parts of the interface: I want to draw them in storyboard, put buttons in that drawings, and connect the buttons to the inherited buttonThing property via an outlet. But wait, the buttonThing property was announced in the superclass and doesn't even appear in the code of the subclasses. So how do I connect the buttons to that outlet?
You can ctrl-drag the UIButton from your storyboard to your BasicController for each subclass you have laid out in your storyboard. You can then use the buttonThing in each subclass as usual.
Remember to link your subviewcontrollers to their respective viewcontrollers in the storyboard.

Using object from one class in another

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

designing a view with interface builder

I'm new to interface builder.
I'd like to design a view in interface builder.
I know I can load the view via 'loadNibNamed'.
Now, suppose there will be two buttons(or two tap gesture recognizers) in the view, and when those buttons are touched, some functions need to fire.
How do I connect(?) the touch to some functions of the viewController that I called the 'loadNibNamed' from?
The view will be a pop-up view (PopUp view) with two buttons(or tap gesture recognizers).
You need to set up outlets from your interface builder objects (buttons) to your class.
To do this, you need to make your class that calls "loadNibNamed" as a delegate class. Then synthesize the button so it is a property.
Once you do that add the selector:
[button addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
The better way would be to write a new class for that particular view. Then if you want to catch the event in the class that called "loadNibNamed" you can set the target to something else:
[button addTarget:(UIMyClass)sender action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
There should be lots of tutorials out there :) - btw have you considered upgrading your xcode and using storyboards?
--EDIT:--
The problem happens because you are mixing coding with interface building. I think you still need to make a viewcontroller class for your new view, but you can change the target of the selector to the calling class.
See more here about selectors: #selector and other class (Objective-C)
There is a similar question here about pushing data to a new view programatically:
Can we pass a parameter to view did load or view will appear of other class from a class
If you're new to IB there are three steps.
Add the method name in your .h (header) file, i.e.
-(IBAction)someMethod:(id) sender;
Save the modified .h file.
Open Interface Builder.
Open the File Owner window under the Tools menu I believe.
Cntrl+Click on the File Owner selection under the File Owner Window.
A black window should appear with a bunch of interface options under it. A small black circle should appear next to someMethod
Drag that small black circle (a blue line should appear) onto the button you want your method to connect to. Another menu should appear. Choose an appropriate action (something like on touch or something like that). Repeat for the second button.
In the IB for each button give them a tag in the attributes list under the Properties (I believe). If you need the Properties window look under the Tools menu again. Go to the Tag section and give each button a different tag. (1, 2, 3 ... etc).
Implement the method in the .m file. Make sure you differentiate your actions for which button the user selects, i.e. if (sender.tag == 1) {...} else if (sender.tag == 2) {...}
Save and run.
The end.