Disable UIButton in another UIViewController - objective-c

I'd like to disable an UIButton in a UITableview in another ViewController:
I tried, inside the SecondViewController, but it only disable the _buttonDesc, buttonCell still is enabled:
buttonCell is the Button inside of UITableView.
buttonDesc is the Button Comprar, inside the SecondViewController.
.
-(IBAction)comprar
{
[_buttonDesc setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
[_buttonDesc setEnabled:NO];
LivroCell *lvc = [[LivroCell alloc]init];
[lvc.buttonCell setEnabled:NO];
}

You need a reference to the actual buttonCell you want to disable. Creating a new instance of LivroCell will not help you.
The easiest way to do this is when someone clicks the "Buy" button, and you create the new UIViewController, pass a reference to the buy button (if "Buy" calls an IBAction then pass sender to the child view controller you create). So make a property on your child view controller to store the button, set that property when you do alloc/init, then it's easy to disable later.

Related

Capture my uibuttons event

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
}

Losing Segue Action when adding custom image in navigation bar button item

I`m developing a app using Storyboards.
In one ViewController I have a button on navigationBar that links to the second ViewController. This transiction is defined in the storyboard (in this case I have defined a push segue to link the two ViewControllers)
I have changed the image of the button following this post in Stackoverflow.
But the problem is: That change in the View of the button breaks the push segue that I have defined in the storyboard. So the question is: How to still change the background of the BarButton without killing the segue action?
I dont want to programmatically reset the segue using performSegueWithIdentifier. This makes no sense since I already have defined it on the storyboard, so I think that must be another solution.
I think this is going to be your best solution:
In viewDidLoad:
self.navButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.navButton.frame = CGRectMake(0, 0, 30, 30);
[self.navButton setImage:[UIImage imageNamed:#"yourImage"] forState:UIControlStateNormal];
[self.navButton addTarget:self action:#selector(yourNavButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *logOutBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.navButton];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObject:navButton, nil]];
And in the method handling your navButton tap (yourNavButtonAction from above)
[self performSegueWithIdentifier:#"yourSegueIdentifier" sender:self];
**Note that this will require you to create a storyboard segue that originates from your ViewController itself, as opposed to a button on that ViewController. Control drag from your ViewController to the target ViewController, give the resulting segue an identifier (yourSegueIdentifier above) and you're set.
You cannot use the quoted code and use the segue at the same time. The code introduced a new object, a UIButton that gets the click, so your storyboard object will not get it any more.
You could try adding a standard custom UIButton in storyboard and change the code as follows:
// instead of
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
// use
_someButton.frame = frameimg;
Assuming that your _someButton is the name for the IBOutlet of the button from storyboard.
If this does not work, you should go with performSegueWithIdentifier. I do not see the problem with this, either. You anyway include a #selector with the custom UIButton. Just use it to initiate the segue. You can still configure the segue, etc., in the storyboard, so nothing is lost, right? In fact, the refactoring above seems like more work.

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.

UIView inside a UIViewController or better way to do it?

I have a problem on how to properly do a certain kind of action.
The image below shows a UIViewController, but the second part of the view is a custom UIView (the one with the profile pic, name and Show View button).
The subclassed UIView is allocated using this code:
profileView = [[GPProfileView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
profileView.myTripGradientColor = YES;
[self.view addSubview:profileView];
The problem is of course, that the button on the UIView can't show any view, since it's only the UIViewController that can push another ViewController to the window(correct?).
The UIView is used in several places in the app and needs to be added easily and have the same behavior across the application.
This worked great until I added the button, and I'm starting to think I've made this wrong, and there has to be a better way to do it (maybe change the UIView to something else?).
I was thinking I should be able to call:
self.superview
And then somehow get the ViewController so I can push another ViewController into the view hierarchy, but nope.
Any suggestions and a tips on how to do this correctly?
UPDATE:
I have no idea on how to push another UIViewController from the button.
What should I do in this method when pressing the button in the UIView:
- (void) showViewButtonTouched:(UIButton*)sender {
GPProfileSocialFriendsViewController *friendsSettings = [[GPProfileSocialFriendsViewController alloc] init];
}
How do I push GPProfileSocialFriendsViewController?
Your - (void) showViewButtonTouched:(UIButton*)sender method should be in your controller and would probably be better named - (void) showView:(UIButton*)sender or - (void) showProfile:(UIButton*)sender so it clearly denotes what it does (not how you got there).
It's not the view's responsibility to manage transitions from a state to another. If you move your method to your controller, your problem is no more (you can easily access self.navigationController or push directly if you don't have an navigation controller like this:
[self presentViewController:vcThatNeedsToBePushed animated:YES completion:nil];
I think you can create weak reference in GPProfileView on UIViewController. Like this:
#property (weak, nonatomic) UIViewController *rootController;
when you create GPProfileView, assign rootController-property:
profileView = [[GPProfileView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
profileView.myTripGradientColor = YES;
profileView.rootController = self; // if view created in view controller
[self.view addSubview:profileView];
and in implementation of button selector:
self.rootController push... // or pop
May be this not correct, but you can try
You could let the view controller push the next view controller when the button is pushed. The view controller can add a target/action on the button, so that the action method in the view controller is called on the touch up inside event.

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: