Connecting event handler to custom UIView component - ios

I have a custom UIView component called ControlsDockView and I have a couple of buttons in the view. I know that you can add the click event for the buttons directly to the UIViewController and proceed further. But would it be possible for me to add the event handler to the ControlsDockView? If so, how?
For android, I know we can directly have the event handler written in the view but is it different with swift?
Do I have to go through the view controller for it? Maybe it is a newbie question but I need to know how I can proceed with this.
Thanks in advance for your help

Yes. You can create an action for a button in your custom view. Easiest way is probably to ctrl-drag from interface builder to your view code.
you will then get an IBAction func actionHandler(sender: UIButton) {} in your view code.
Another way to set this up is by adding the action to the buttons in your loadFromNib method or constructor in the custom view.
However, be careful about adding any kind of logic that isn't directly related to the view in those actions as it's considered an extremely bad coding practise to add business logic to you views in such a manner.

Related

Duplicate Button actions on different View Controllers?

I have a button on one of my view controllers which controls a score action (counter).
Can someone help me duplicating the button and the action it takes and place the duplicate on a separate ViewController?
I need this for a workout Application i'm making and I want to get the action Proof of concept complete before I implement it into my App.
I have attached my Xcode Project with the stage I am now on.
Ive tried passing the name of the action button from the main VC to the newest but I've had no luck.
https://github.com/Mulreany93/NewVCActionButton
I saw in your GitHub that you have an empty class called ViewControllerNewButton, which inherits from UIViewController. I suggest making your scorebutton into a new class called something like e.g. ScoreButton, which should inherit directly from UIButton - class ScoreButton: UIButton {...} Then in each View Controller you can create your scorebutton: let scoreButtonTest = ScoreButton() and then you can use e.g. conversion functions like scoreButtonTest.someConversion() from your ScoreButton class.
If I am on the wrong track, feel free to comment but as far as I can tell you are just trying to use your button in multiple VCs and that's a good place to try inheritance.
Edit for clarification:
class ScoreButton: UIButton {
func someConversion() {
...
}
}

Check which UIViewController my custom button class is on w/o Storyboard

I have created a custom class for my UIBarButtonItem (refreshIndicator.m). This button will be on many different view controllers, all push-segued from my MainViewController/NavigationController.
Instead of dragging an outlet onto every single ViewController.m file for iPhone storyboard THEN iPad storyboard (ugh, still targeting iOS7), I want to know if there is a way to complete my task simply within my UIBarButtonItem custom class. I've looked around everywhere but I haven't quite found an answer to this,
All I need to do is check which UIViewController is present, check the last time the page was refreshed, and then based on that time, set an image for the UIBarButtonItem. (I've got this part figured out though, unless someone has a better suggestion). How can I check for the current UIViewController within a custom button class? Is this possible?
Does it need to know which view controller its on so it can tell that vc it was pressed? If that's the case, then use your button's inherited target and action properties. On every vc that contains an instance of the button, in view did load:
self.myRefreshIndicator.target = self;
self.myRefreshIndicator.action = #selector(myRefreshIndicatorTapped:);
- (void)myRefreshIndicatorTapped:(id)sender {
// do whatever
}
More generally, its better to have knowledge about the model flow to the views from the vc, and knowledge of user actions flow from the views. Under that principal, your custom button could have a method like:
- (void)timeIntervalSinceLastRefresh:(NSTimeInterval)seconds {
// change how I look based on how many seconds are passed
}
And your vcs:
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:self.lastRefreshDate];
[self.myRefreshIndicator timeIntervalSinceLastRefresh:interval];
If you really must go from a subview to a view controller, you could follow the responder chain as suggested in a few of the answers here (but I would go to great lengths to avoid this sort of thing).
It is possible to achieve this, but the solution is everything but elegant. It is one way of getting around the basic principles of iOS and is strongly discouraged.
One of the ways is to walk through the responder chain, posted by Phil M.
Another way is to look through all subviews of view controllers until you find the button.
Both ways are considered a bad practice and should be avoided.
For your particular case, I would rethink the structure of having a separate instance of the bar button. For example, you could rework it into a single UIButton instance that gets displayed over every view controller and it can also act as a singleton.

Design iOS View in Code without storyboard

I want to design and generate view elements in code, without using storyboard. But I haven't figure out a simple way to locate where should I put my view elements by describing its CGRect. For example, I can drag a button direct into the storyboard and put it where i want, however, in code, I have to write something like
[button setFrame:CGRectMake(24.0f, 113.0f, 271.0f, 140.0f)];
Is there any way or any software can help me?
Your question is a bit broad. You can create your view in code by including in your subclass of UIViewController properties for each subview (control) element and then make all the initialisation in designated initialiser and setting frames and other customisation in viewDidLoad method most likely. But for details where to what set you should go to references for UIViewController lifecycle as it may depend on what you want to achieve. in some cases setting frames in viewDidLoad will be fine whereas sometimes you will need to make some changes in viewDidLayoutSubviews for example.

How to create new objects consisting of multiple cocoa touch objects?

I want to make a group of cocoa touch objects behave like one object, Is this possible?
For instance, say we had a SuperButton that consisted of two UIButtons, spaced by 10.
Let's just say their frame is set.
Is it possible to make a wrapper class ?
that I can init and call -
[_view addSubview:SuperButton] on and have it create both buttons?
I'm not entirely sure on the terminology for what I'm trying to do so it's hard to find any help on google.
Okay, Let me ask you this question first. So you want to create a SuperButton, when ever you call it it should layout the two buttons for you, is this right ?. YES you can do this. Create a new XIB, which is having just a view. configure your buttons in it. Create your super button class and assign it to this xib. You should be able to call this class and it will return view which can be added to your main view controller.

Not all UITextFields are calling all delegate methods

I've got a view controller xib file with several views in it. I'm building a wizard-type interface. I'm just doing a simple fade between the views, and I'm already using a navigation controller for the main interface. I'd prefer not to use one for this wizard. Anyway, in the views, each panel has at least a button, some sort of input field (usually a UITextField) and some helper text hard coded in a UILabel.
The problem is that not all the UITextField objects are calling the textFieldDidChange method in the delegate (File's Owner - .m file associated with the xib), but all the UITextField objects ARE calling the textFieldDidBeginEditing method.
Makes no sense to me. I feel like I must be missing something simple in how I set up the screens, but I'll be darned if I can figure it out. Each of the screens all look identical in the property sheets (on the right hand side of Xcode), and everything is wired up correctly in the File's Owner property sheet, both in IBOutlet and IBActions.
Here are some shots of what's going on...
Ideas? Thanks.
Here are links to the screen caps of the vital parts.
(being a new member is making it hard to add all the info I need with screen caps!)
As far as I now, there is no delegate method with the header textFieldDidChange. You have created a method of your own, which is depending on a NSNotification. Make sure all the UITextFields are send the right notification.
There is no such method on a UITextFieldDelegate
You may have confused textViewDidChange, which is a delegate method for a UITextView, but itis passed the UITextView that generated the event, not an NSNotification.
Seems like you want textField:shouldChangeCharactersInRange:replacementString: instead.
This is resolved. I'm a knucklehead. :-)
I was attaching my own notifier/observer and hadn't done so for the last few UITextField objects. Sorry to bother y'all.

Resources