I need to disable a button for 2.56 seconds, long after control is returned to the user. I am using the following code where theButton is type UIButton, defined at the beginning of the routine with
UIButton *theButton = sender;
and later calling the following which will update a label and then, hopefully, enable the button. Updating the label works perfectly but it crashes when trying to enable the button so I must be passing the UIButton incorrectly. Can anyone give me an example / correct me on this?
[NSTimer scheduledTimerWithTimeInterval:2.56
target:self
selector:#selector(updateLabel:)
userInfo:theButton
repeats:NO];
Thanks for your help...
I hope you are writing updateLabel: method as follows
-(void) updateLabel:(NSTimer *)timer1
{
//your other code...
[(UIButton *)[timer1 userInfo] setEnabled:YES];
//your other code
}
Thanks,
Related
how can I implement "Next" and "Previous" Button's over the keyboard.
I use UITextView and with click on "Next" the Next TextView gets focus, Previous the last TextView.
I do not found any tutorial on the Internet.
I hope everybody can help me.
Please use this custom control
COCOA
If you need any help, let me know.
This controlis very simple to use.
Updated Answer
Change the Button Label
Go to BSKeyboardControls.m file & make the following Changes
On line no. 42
[self setSegmentedControl:[[UISegmentedControl alloc] initWithItems:#[ NSLocalizedStringFromTable(#"<", #"BSKeyboardControls", #"Previous button title."),
NSLocalizedStringFromTable(#">", #"BSKeyboardControls", #"Next button title.") ]]];
On line No. 51
[self setDoneButton:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(#"Ok", #"BSKeyboardControls", #"Done button title.")
style:UIBarButtonItemStyleDone
target:self
action:#selector(doneButtonPressed:)]];
///// ********* Update 2 ********* ////////////
SET Image for Previous & Next
Just add the following code in BSKeyboardControls.m after line no. 52
[self.segmentedControl setImage:[UIImage imageNamed:#"add.png"] forSegmentAtIndex:BSKeyboardControlsDirectionPrevious];
[self.segmentedControl setImage:[UIImage imageNamed:#"add.png"] forSegmentAtIndex:BSKeyboardControlsDirectionNext];
change BSKeyboardControls.m file & make the following Changes
remove this one
// [self setRightArrowButton:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:self action:#selector(selectNextField)]];
add this
[self setRightArrowButton:[[UIBarButtonItem alloc]initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(selectNextField)]];
I'm currently working on an app where the user has the option to either swipe through data, or use a button to go through the data. I'm having trouble understanding how to combine two bits of code.
Here is the code I'm using for swiping:
- (void)swipeRight:(UISwipeGestureRecognizer*)recognizer
{
if ([questions hasPrevQuestion] == YES) {
[self vorige:nil];
}
}
(the [self vorige:nil]; is calling the method for the button, so the swiping and the button have the same behavior)
and I need to somehow incorporate this code which applies to the button:
-(void)animationDidEndOnAnswer {
[vorigeButton addTarget:self action:#selector(newQuestion:) forControlEvents:UIControlEventTouchUpInside];
}
I think it's pretty simple, but I just cannot for the life of me figure out how to call the swiping method place of the button here...I'm thinking it's simple because I found this example in the UIGestureRecognizer class reference:
- (void)addTarget:(id)target action:(SEL)action
but being new to objective-c, I don't really know what to do with this. any help is very appreciated.
- (void)addTarget:(id)target action:(SEL)action is the code equivalent of binding a button action to a method like you do when you ctrl-drag from a button to an IBAction in Interface Builder.
So if your method is called vorige: and you want it to be called when the button is tapped, you would say:
[vorigeButton addTarget:self action:#selector(vorige:) forControlEvents:UIControlEventTouchUpInside];
But I don't know why you would want to do that as a result of an animation - you normally would set the button action once when the view is loaded, not change it during an animation.
Nick's solution is good.
if you want to call the same method for the swiping & the tap on your button, you can tweak your swipeRight like this:
- (void)goThrougtData:(id)sender
{
if( [sender isKindOfClass:[UISwipeGestureRecognizer class]] ) {
// swipe specific code
}
else if( [sender isKindOfClass:[UIButton class]] ) {
// tap specific code
}
if ([questions hasPrevQuestion] == YES) {
[self vorige:nil];
}
}
and in your init method, you add
[mySwipeRecognizer addTarget:self action:#selector(vorige:)];
[vorigeButton addTarget:self action:#selector(vorige:) forControlEvents:UIControlEventTouchUpInside];
I my application i have added button in NavigationBar like this..
UIBarButtonItem *more=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"search-25by25.png"] style:UIBarButtonItemStylePlain target:self action:#selector(SelectMission:)];
self.navigationItem.rightBarButtonItem = more;
When i am clicking on button application get's shutdown...
If i am doing same thing with normal button it's working fine can any one help me why it's behaving like this?
Try This
UIImage *i=[UIImage
imageNamed:#"search-25by25.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.bounds = CGRectMake( 0, 0, i.size.width, i.size.height );
[myButton setImage:i forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(SelectMission:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *more=[[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem
= more;
hope it helps :)
Have you looked in the code for SelectMission:? The code you've posted is only for presenting the button, which from your description appears to be working.
Also if there's anything being dumped into the console (Command-Shift-R)?
Judging by the crash log in your comment, I would say this has nothing to do with the UIBarButtonItem class in particular, and everything to do with your action handler. The crash logs tell the whole story: Your class does not implement a method called SelectMission: that takes one argument. Some caveats about the #selector keyword that you will want to double check:
1) Capitalization. Make sure that the method you implement is SelectMission:. Not selectMission:, selectmission:, Selectmission:, etc.
2) Arguments. The colon indicates that the method SelectMission: takes one argument. If you have implemented it and forgotten the argument it will crash with the exception you posted.
That should help narrow down the issue.
I have the following code
-(IBAction)ATapped:(id)sender{
//want some way to hide the button which is tapped
self.hidden = YES;
}
Which is linked to multiple buttons. I want to hide the button which triggered this IBAction.
self.hidden is obviously not the button.
How do I hide the button which was tapped? The sender.
Thanks
Both Vladimir and Henrik's answers would be correct. Don't let the 'id' type scare you. It's still your button object it's just that the compiler doesn't know what the type is. As such you can't reference properties on it unless it is cast to a specific type (Henrik's answer).
-(IBAction)ATapped:(id)sender{
// Possible Cast
UIButton* myButton = (UIButton*)sender;
myButton.hidden = YES;
}
Or you can send any message (call any method) on the object, assuming YOU know the type (which you do, it's a button), without having to cast (Vladimir's answer).
-(IBAction)ATapped:(id)sender{
//want some way to hide the button which is tapped
[sender setHidden:YES];
}
Send setHidden message to sender:
-(IBAction)ATapped:(id)sender{
//want some way to hide the button which is tapped
[sender setHidden:YES];
}
Your getting the button object (id) provided as a parameter
-(IBAction)ATapped:(id)sender{
// Possible Cast
UIButton* myButton = (UIButton*)sender;
myButton.hidden = YES;
}
If you want bullet proof cast/messaging, try this:
-(IBAction)ATapped:(id)sender{
// Secure Cast of sender to UIButton
if ([sender isKindOfClass:[UIButton class]]) {
UIButton* myButton = (UIButton*)sender;
myButton.hidden = YES;
}
}
And... if you want to change the backgroundcolor of a button, the correct code will be like this?
[sender setBackgroundColor:(NSColor *)redColor];
for example? ... because it isĀ“nt works for my...
I tried just making the image switch to black and then use sleep(1) and have it go back to the original image, but the sleep doesn't work at the right time, and I can't even see the black flash it goes so fast.
[blueButton setImage:[UIImage imageNamed:#"black.png"] forState:UIControlStateNormal];
sleep(3);
[blueButton setImage:[UIImage imageNamed:#"blue.png"] forState:UIControlStateNormal];
I just want to make it give a indicator to this button. Any thoughts? Thanks.
I think that using NSTimer for this case is overkill.
UIButton exposes a convenient property named imageView that give you access to the underlying layer.
Assuming that your button is called recButton you can prepare the animation as follow:
recButton.imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"recButtonOFF"], [UIImage imageNamed:#"recButtonON"], nil];
recButton.imageView.animationRepeatCount = 0;
recButton.imageView.animationDuration = 1.0;
and then start/stop animation in your designed selector with the following:
[recButton.imageView startAnimating]; //start the animation
...
[recButton.imageView stopAnimating]; //stop the animation
Call a timer to notify you after a given time intercal:
[+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats][1]
Give it a simple method as the selector, which changes the image back to blue.
[blueButton setImage:[UIImage imageNamed:#"black.png"] forState:UIControlStateNormal];
[NSTimer timerWithTimeInterval:yourTimeInterval target:self selector:#selector(changeButtonBack:) userInfo:nil repeats:NO];
-(void) changeButtonBack:(id)sender{
[blueButton setImage:[UIImage imageNamed:#"blue.png"] forState:UIControlStateNormal];
}
Now this method will be called after whatever time interval you specify, and the button will go back to blue!
Note: After you make this call, your code continues executing. This is called setting up a call-back, because your code keeps on executing, and after the specified time, the system calls you back and tells you to execute the specified function.
If you want to have different behaviour in different places, you could either specify different selectors (read:methods to be called-back), or you could pass in an object of some sort as the userInfor argument, which I set as nil in the example. Then when you get called back, check the value of the userInfo object and do what you want based on what it is.
You need to use a NSTimer so that you change the image, then schedule a timer to change the image again 3 seconds later.
The reason sleep(3) doesn't work is because the image update doesn't happen until control returns to the run loop, after you've returned from your method (function).