Assigning a callback to a UIView's UIButton from the containing UIViewController - ios

Right now I have a UIViewController subclass that is hosting a UIView subclass. This UIView has a UIButton, and I want the touch event to be sent to the UIViewController subclass. Right now I have a getter method in the UIView subclass, so I grab the button and assign it by just using the reference from within the UIViewController subclass.
What's the best approach to achieve this result?

Add the action event to your ViewController
- (IBAction)myButtonPressed:sender;
Now if you're using IB (you should be) just link it up, or if doing this in code then manually create the event link.
[myButton addTarget:self action:#selector(myButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

Related

UIControl not receiving touches inside UITableView Cell

I added UIView in storyboard & make it as UIControl's subview. This view is inside UITableViewCell. Now I want to add touch up inside action for UIControl. I have added below code:
[cell.conditionControl addTarget:self action:#selector(btnConditionClick:) forControlEvents:UIControlEventTouchUpInside];
But this code is not working & not receiving any events.
I also disable userInteractionEnabled flag for UIView's Subview.

Should i create a view ( consisting UIButton, UILabel etc) in a separate UIView class or inside UIViewController?

I have a UIViewController say viewControllerA which contains some view element like UIButton, UILabel etc. Now my question is should I create those view elements in a separate UIView class and then add in UIViewController, or should I create those view elements directly inside the UIViewController. Accordingly to MVC isn't it appropriate to create view elements inside a separate UIView class and then add this in UIViewController?
The standard place to build the view hierarchy in a UIViewController is in the -viewDidLoad method. That method gets called whenever the UIViewController's view is created. The view controller's view will be loaded from the NIB/Storyboard if applicable; your outlets will be wired up; and then -viewDidLoad is called for you to perform further customization:
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,100.0,40.0)];
[self.view addSubView:aLabel];
}
In Cocoa/Cocoa Touch you don't always want to subclass everything the way you would in, say, Java. There are often other preferred means of extending the functionality in built-in classes such as Objective-C categories, delegation, and pre-defined properties.
It's certainly possible to do this sort of thing another way, but this is the most "Cocoa-like" way to do it. Actually, the most "Cocoa-like" way would be to create the view hierarchy in Interface Builder, but if you want to do it programmatically this is the usual way.

Defining custom UIViews in storyboard

I want to show my own custom UIView in storyboard. By far I have done following but my custom view is not showing up.
Dragged and dropped a UIView instance in my screen.
Defined the class for this UIView as my custom class.
Have connected this UIView with an IBOutlet in my view controller.
I even tried with below code in viewWillAppear.
self.myView = [[MyCustomView alloc] initWithFrame:frame];
This works if I create an instance of my custom view and add as a subview to my IBOutlet property for my view. So, below code is working but I want to keep track of only my IBOutlet iVar and do not want to play with another object for changes on my custom view:
self.myExtraView = [[MyCustomView alloc] initWithFrame:frame];
[self.myView addSubview:self.myExtraView];
Any idea how to do this in a better way so that I could have just one reference of my custom view and could change properties on it as per will.
Found the issue. With storyboard we must initialize anything in initWithCode method. I was implementing the regular init method.

How to instantiate a Custom UIButton in UITableViewCell

I have a UITableViewCell and my cells contain a UIButton subclass (CoolButton).
I have picked the code of CoolButton from raywenderlich tutorial
available here
In the IB I have created a subclass of the UITableViewCell and I have configured the class of my button to CoolButton and set the property type to Custom and configured the IBOutlet for the CoolButton to my UITableView subclass.
My problem is when cells are created, they get a UIButton instead of a CoolButton. As a consequence when I'm setting the properties of the CoolButton I get a crash with "unrecognized selector".
Why UIButton is instantiated instead of a CoolButton? How can I get my CoolButton in the table?
Thanks,
Séb.
I'm going to guess you are doing something like:
CoolButton *b = [UIButton ...]
You need to create it using the CoolButton class:
CoolButton *b = [CoolButton ....];
Or, if you used IB, that you created a UIButton but used an IBOutlet of CoolButton. You would need to make the actual class of the button you drag into IB CoolButton, which you can do by selecting the button and tapping (I think) the second to the left top tab in the inspector.

Call presentModalView controller in UIView

I want to call the presentModalViewController on UIView.
I want to create one model class subclass of UIView and create 3 buttons with action and then add it to the UIView. I wrote a small code about sending a message. In that I call presentModalViewController and I add that view to firstViewController.
Is it possible?
I wrote the code in AppDelegate->application:didReceiveLocalNotification
After receiving a notification this view will be added to MainViewController
NotifViewModel *remainderAlert = [[NotifViewModel alloc]initWithFrame:CGRectMake(40, 60, 250, 300)];
[remainderAlert showRemainderAlert1];
[self.viewController.view addSubview:remainderAlert];
No, you can't call presentModalViewController on UIView.
You only can 'presentModalViewController' from UIViewController or it's subclass (UINavigationController, UITableViewController, etc).

Resources