UIButton strange behaviour - objective-c

I have created a button
uploadBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
uploadBtn.frame = CGRectMake(35, 340, 250, 40);
[uploadBtn setTitle:#"Upload" forState:UIControlStateNormal];
[uploadBtn addTarget:self action:#selector(callUpload) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:uploadBtn];
for action
-(void)callUpload
{
UploadViewController *uploadObj = [[UploadViewController alloc]init];
[self.navigationController pushViewController:uploadObj animated:YES];
}
but when i tap the button no event is firing up. Please help

Btn's method should have its sender included.
Update this line.
[uploadBtn addTarget:self action:#selector(callUpload:) forControlEvents:UIControlEventTouchUpInside];
And calling method with,
-(void)callUpload : (id) sender
{
UploadViewController *uploadObj = [[UploadViewController alloc]init];
[self.navigationController pushViewController:uploadObj animated:YES];
}

Related

Remove buttons and labels from SuperView?

In .m File
#interface ReaderViewController ()
{
UIButton *button;
}
Then I create
Two buttons using the button Object
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=-1;
[button addTarget:self
action:#selector(record:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Rec/Stop" forState:UIControlStateNormal];
button.frame = CGRectMake(215, 110, 80, 50);
[self.view addSubview:button];
Then again I am creating one more button usingbutton object
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=1;
[button addTarget:self
action:#selector(record:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Rec/Stop" forState:UIControlStateNormal];
button.frame = CGRectMake(340, 110, 80, 50);
[self.view addSubview:button];
Now I want to remove both buttons from superView??
How can i do that
I've tried this it didn't work [button removeFromSuperview];
Try this:
for (UIView* subV in self.view.subviews) {
if ([subV isKindOfClass:[UIButton class]])
[subV removeFromSuperview];
}
NB: this remove all buttons into you superview
Set meaningful tag values for the buttons:
button.tag = 1000;
and
button.tag = 1001;
and then you can remove the buttons with:
[[self.view viewWithTag:1000] removeFromSuperview];
[[self.view viewWithTag:1001] removeFromSuperview];
NOTE: You don't need the instance variable button; you can simply use a local variable instead.

Button action doesn't respond when switched viewcontroller

I have an app where the viewcontroller is switched if a button is pressed. I switch like this:
NewViewController *nv = [[NewViewController alloc] init];
[nv NewButtonTapped:self];
[self.view addSubview:nv.view];
Then in my NewViewController.m:
- (void)NewButtonTapped:(id)sender
{
backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];
backgroundImage.image = [UIImage imageNamed:#"cubePage.png"];
[self.view addSubview:backgroundImage];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(964, 100, 60, 150);
button.enabled = YES;
button.userInteractionEnabled = YES;
[button setTitle:#"D" forState:UIControlStateNormal];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonTapped:(id)sender
{
NSLog(#"Tapped");
}
All images and buttons are loaded and appears fine in the new viewcontroller but the button action buttonTapped: doesn't respond when I'm tapping the button. This code works fine in the first viewcontroller.What's the problem?

How does work viewWillAppear?

My viewWillAppear method call "-(void)doSomething".
- (void)doSomething
{
Y4AppDelegate * delegate = (Y4AppDelegate *)[[UIApplication sharedApplication] delegate];
if(delegate.booSomeValue == 0) {
UIButton * aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setFrame:CGRectMake(20,360,280,40)];
[aButton setTitle:#"Title"
forState:UIControlStateNormal];
[aButton addTarget:self
action:#selector(mySelector)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:aButton];
}
}
It work, but aButton is still visible. What can I do to hide aButton? I have three UIViewController. In third i set delegate.booSomeValue to true. When I come back to previous UIViewController, I call this viewWillAppear but aButton is visible. I want to hide it.
The problem is that you added it once, and when you go back to it, you are not adding a second one, but the first one you added is still there, so you have to remove it.
To do so, you will need first to create a property to store the button, and check if it exists
if ( ... show button condition ... ) {
if (!aButton) {
... create and show button ...
}
}
else {
if (aButton) {
[aButton removeFromSuperview];
aButton = nil;
}
}
Move this code to viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad]
UIButton * aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.tag = 101;
[aButton setFrame:CGRectMake(20,360,280,40)];
[aButton setTitle:#"Title"
forState:UIControlStateNormal];
[aButton addTarget:self
action:#selector(mySelector)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:aButton];
}
And
- (void)doSomething
{
Y4AppDelegate * delegate = (Y4AppDelegate *)[[UIApplication sharedApplication] delegate];
UIButton * aButton = (UIButton*)[self.view viewWithTag:101];
aButton.hidden = delegate.booSomeValue;
}

Button's action causes "invalid selector" crash -- why?

This code results in an "invalid selector" error when the button I create is pressed. Where is the test function fetched from?
Main.m
mainScreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
[self.view addSubview:mainScreen];
TaskButtons *tB = [[TaskButtons alloc] init];
[mainScreen addSubview:[tB TaskStart]];
TaskButtons.m
- (UIButton*)TaskStart {
CGRect buttonFrame = CGRectMake(500, 206, 400, 35);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonFrame;
[button setTitle:#"Task Button" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.titleLabel.textAlignment = UITextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(test) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (void)test{
NSLog(#"test line");
}
It seems that the test function isn't being called. Doesn't setting the button's target to self here mean it should look in the TaskButtons class for the function called test?
The problem is ARC is releasing the instantiated object too soon. So to solve this I would need to retain it longer.
Main.h
#import "TaskButtons.m"
#interface ViewController : UIViewController {
TaskButtons *tB;
}
#property (nonatomic, retain) TaskButtons *tB;
[button addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchUpInside];
- (void)test:(id)sender{
NSLog(#"test line");
}
Syntax problem :) In your code replace these lines.

UIbutton with longpress and Touchup inside

How to create a UIButton With two actions.
I know by using UILongPressGestureRecognizer we can perform Longpress.
But my requirement is,When I Long Press UIButton,it has to perform one action and when touch
up inside it, it has to perform another action.
Thanks.
Below is my code.
UIImage *redImage = [UIImage imageNamed:#"TabFav2.png"];
tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[tabRedbutton setImage:redImage forState:UIControlStateNormal];
tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35);
redTAb = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton];
[tabRedbutton addTarget:self action:#selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside];
longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
longpressGesture1.minimumPressDuration =0.1;
[longpressGesture1 setDelegate:self];
longpressGesture1.cancelsTouchesInView = NO;
[tabRedbutton addGestureRecognizer:longpressGesture1];
[longpressGesture1 release];
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
if (longpressGesture.state == UIGestureRecognizerStateBegan)
{
NSlog(#"Long press");
}
}
-(void)redbottonmethod
{
NSlog(#"single tapped");
}
For the tap you can use UIButton's "addTarget:..." method and for the longpress you can add a gesture recognizer:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100.0, 100.0, 100.0, 20.0);
[btn setTitle:#"Test" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(userTapped:) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] init];
[gr addTarget:self action:#selector(userLongPressed:)];
[btn addGestureRecognizer:gr];
[gr release];
[self.view addSubview:btn];
Of course you need to implement the 2 methods that will be called:
- (void)userTapped:(id)sender {
NSLog(#"user tapped");
}
- (void)userLongPressed:(id)sender {
NSLog(#"user long pressed");
}
Hope that helps.
=========
EDIT: It seems that you are using your button as a BarButtonItem inside a UIToolbar. So I changed my code to do the same:
- (void)viewDidLoad {
[super viewDidLoad];
// set up the button
UIImage *redImage = [UIImage imageNamed:#"TabFav2.png"];
UIButton *tabRedbutton = [UIButton buttonWithType:UIButtonTypeCustom];
tabRedbutton.backgroundColor = [UIColor redColor];
[tabRedbutton setImage:redImage forState:UIControlStateNormal];
tabRedbutton.frame = CGRectMake(0.0, 0.0, 50,35);
// set up a bar button item with the button as its view
UIBarButtonItem *redTab = [[UIBarButtonItem alloc] initWithCustomView:tabRedbutton];
// set up toolbar and add the button as a bar button item
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 100.0, 768.0, 40.0)];
toolbar.barStyle = UIBarStyleBlack;
NSArray *items = [NSArray arrayWithObject:redTab];
[toolbar setItems:items];
[self.view addSubview:toolbar];
[toolbar release];
// add tap handler to button for tap
[tabRedbutton addTarget:self action:#selector(redbottonmethod) forControlEvents:UIControlEventTouchUpInside];
// add gesture recognizer to button for longpress
UILongPressGestureRecognizer *longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressHandler:)];
longpressGesture1.minimumPressDuration =0.1;
[tabRedbutton addGestureRecognizer:longpressGesture1];
[longpressGesture1 release];
}
And the two methods that get called:
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(#"Long press");
}
-(void)redbottonmethod {
NSLog(#"single tapped");
}
This code definitely works.
By the way: I noticed that in your code in the 2 methods that get called you have typo: You must use NSLog() and not NSlog(). Could that be the problem?