Capture my uibuttons event - ios7

I have a big problem in my iphone/ipad ios7 app, I have a lot of controls based on UIView.
for example
list in my view controller I'm adding list based on UIView, this list contains some controls subviews based on UIView, and this controls have a lot subviews (particulary uibuttons) too. And now I want get UIControlEventTouchUpInside action im my viewcontroller, how I can do that ? I Do delegate im my uibutton control but im my view controller I dont't have instance this button, so I can't use
myButton.delegate = self;
I Have just instance my SuperView.
Someone could help me?

It will be better if you use custom view class for your UIView which is you used for containing your button and other controls. In that custom class you can simply set the action for your controls.
EDIT
You can use the following method in your UIButton's custom class:
- (void)awakeFromNib {
[super awakeFromNib];
[self addTarget:self
action:#selector(yourClickMethod:)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)yourClickMethod:(UIButton *)infoButton {
// Button action goes here
}

Related

NSView redraw not working with setNeedsDisplay:YES;

I have a custom view on which I want to force a redraw upon execution of an IBAction method implemented in the NSView controller subclass:
- (IBAction)forceRedraw:(id)sender {
[self setNeedsDisplay:YES];
[self display];
}
This seems to have no direct effect whatsoever. My custom view only gets redrawn once I mouse into the custom view area or resize the main window.
Any thoughts on why this isn't working?

Detecting UIButton touches on a subview

I have added a subview which contains a UIButton control. This subview is added to its superview programatically. I need to perform an action when this button is tapped, but since it's contained within its own subview, I can't hook an IBAction up to the view controller in order to push another view controller.
Is there an easy way to detect that the button is tapped and call a method within its super view?
You can do everything programmatically:
[buttonName addTarget:self action:#selector(methodName:) forControlEvents:UIControlEventTouchUpInside];
and then create your method:
- (void)methodName:(id)sender
{
// Do something.
}
This is all you have to code.

How to implement more than one controller in objective C

First of all I don't know If controller is the right word. What I want to achieve is this.
#interface ClubViewController : CoreDataTableViewController :NRGridViewController
{
I know that this is not possible in objective-C. But is there a way to work around this?
Because I want to use CoreDateTableViewController and NRGridViewController.
Kind regards
Stef
EDIT
This is how my storyboard Hierarchy looks like.
-ViewController
-TableView
-View
-TableViewCell
So I have a tableview Controller but above this tableview controller you find a small view with three buttons. When I push on button 1 I want to take the tableview away and draw a gridView with the NRGridview Controller. But when I push on button 2 and 3 I fill up my tableview using the CoreDataTableViewController.
I hope this explains more my problem.
I think one way to do this is with a container view with a container view controller inside it. That container controller would have 2 child controllers which would be your CoreDateTableViewController and NRGridViewController. I've implemented something like this, and I can show you some code if you're interested.
After Edit: In a test app, I started with a single view template and a storyboard. I added two buttons to the top of the view and a container view to the bottom half of the view (this first controller is of class ViewController). I then dragged out a new view controller, and control dragged from the container view to the new controller and chose the "embed segue" (this will resize the view to be the same size as the container view). The class of this controller was changed to my subclass, ContainerController. I then created 2 more controllers for the 2 views that will be managed by the container controller (the views need to have their size set to "freeform" in IB so you can set the size to be the same as the container view). Here is the code in ContainerController:
- (void)viewDidLoad
{
[super viewDidLoad];
self.cont1 = [[FirstController alloc]initWithNibName:#"FirstView" bundle:nil];
self.cont2 = [[SecondController alloc]initWithNibName:#"SecondController" bundle:nil];
[self addChildViewController:self.cont1];
self.currentController = self.cont1;
[self.view addSubview:self.cont1.view];
}
-(void)switchToFirst {
if (self.currentController != self.cont1) {
[self addChildViewController:self.cont1];
[self moveToNewController:self.cont1];
}
}
-(void)switchToSecond {
if (self.currentController != self.cont2) {
[self addChildViewController:self.cont2];
[self moveToNewController:self.cont2];
}
}
-(void)moveToNewController:(id) newController {
[self.currentController willMoveToParentViewController:nil];
[self transitionFromViewController:self.currentController toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{}
completion:^(BOOL finished) {
[self.currentController removeFromParentViewController];
[newController didMoveToParentViewController:self];
self.currentController = newController;
}];
}
The only code I have in ViewController are the IBActions for the 2 buttons that switch the views. Those methods just call methods in the container controller:
-(IBAction)chooseFirstController:(id)sender {
[self.childViewControllers.lastObject switchToFirst];
}
-(IBAction)chooseSecondController:(id)sender {
[self.childViewControllers.lastObject switchToSecond];
}
What you are trying to do in your code is creating a class that is a subclass of multiple other classes, which is not possible. If you really want to do this, check out this question: Inherit from two classes
If you are trying to create multiple instances:
CoreDataTableViewController and NRGridViewController are just classes, which you will have to instantiate to get an actual object.
You can instantiate e.g. an NRGridViewController using
NRGridViewController *controller=[[NRGridViewController alloc] init];
I hope this answers your question, it is a bit difficult to understand your question.
Instead of taking tableViewController, take normal TableView (drag and drop from the storyboard to the particular position on the view).
when the button 1 is pressed Make the Table View hidden in the buttons action method. and initialize the grid view / or set grid view hidden to NO. (all views have the property of hidden in ios)
when you press on the 2nd and 3rd button set the grid view hidden and set tableview hidden equal to NO. and fetch the coredata and store it in array or dictionary or you can reload the tableview.
(Initially, before pressing the button 2 & 3 , the table view has no values. so you can set a bool property that when you press the button 2 or 3 set the bool and use the bool to reload your tabe view )
if you did not get my explanation ping me back.

Pass data from Viewcontroller to a Label [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Passing Data between View Controllers
iOS dev noob here..
I have a bunch of buttons on my main ViewController, what I want to happen is when the user clicks on one of those buttons, I want it to take them to a separate view that has a label and I want the text in that label to change according to which button they press.
So basically I want to be able to pass data from a button press on the main ViewController to a label in a second view.
That might be a bit confusing and I apologize, but any help would be greatly appreciated.
I would suggest you use a storyboard if possible. You then can ctrl drag from each button to a new viewcontroller that you drag out onto the storyboard. This will setup a segue and you can use the method prepareforseguie to pass the data you need to use in the new view controller.
Assuming your view controller with the buttons is in a navigation controller - presumably at the root...
What you would do is, add a target to each of your buttons, in code this can be done with the method addTarget:action:forControlEvents: of your UIButton
For example:
[myBtn addTarget:self action:#selector(tappedButton:) forControlEvents:UIControlEventTouchUpInside];
The method tappedButton will be messaged with the button that was tapped:
- (void)tappedButton:(UIButton*)sender{
// exploit the button
}
Inside this method you can get the title of the button - myBtn.titleLabel.text
You can then create a new view controller (let's keep things simple and say you have your own UIViewController subclass called MySimpleViewController.
In this class you have a cameFrom property which you can set the button's title on, and in viewDidLoad of MySimpleViewController, you would get the property value of cameFrom, this could be the method implementation.
- (void)tappedButton:(UIButton*)sender{
MySimpleViewController *detail = [[MySimpleViewController alloc] init];
detail.cameFrom = sender.titleLabel.text;
[self.navigationController pushViewController:detail
animated:YES];
[detail release];
}
So over in your MySimpleViewController's viewDidLoad, you create a UILabel, give the text property the value of self.cameFrom and add it to the view with addSubview:

How do I make the keyboard go away when a user clicks on the background of the view?

I have a UITextField in my iOS app. When a user enters text and clicks Return, the keyboard goes away due to a call to an IBAction with "resignFirstResponder."
However, XCode does not let me drag a line from the UIView itself to File Owner. How do I associate touching the background of a UIView with an IBAction that makes the keyboard go away?
You can use UITapGestureRecognizer. see: Dismiss keyboard by touching background of UITableView
so instead of tableview, just add it to your view instead:
UITapGestureRecognizer *gestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)] autorelease];
gestureRecognizer.cancelsTouchesInView = NO; //so that action such as clear text field button can be pressed
[self.view addGestureRecognizer:gestureRecognizer];
and have a method to hide your keyboard
- (void) hideKeyboard {
[self.view endEditing:YES];
}
You've already noticed that you can't drag from the UIView to the file's owner to assoctiate an action with a touch.
The way to work around this is to change the class of the background view from UIView to UIControl and hook up an action from there to a method in your controller to stop editing.
That's because a UIControl can respond to touch events, and a UIView does not, but a UIControl subclasses UIView, and so it can be used in place of a UIView.
I wrote an example project a while ago that uses this technique. Have a look at the secondViewController's xib file and see how I've change the class of the background view and hooked it up to a an action in the controller to dismiss the keyboard.
Use the touchesBegan with Event and end editing on the view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
One easy way to do it is to create a big transparent UIButton behind the view.