I am trying to print "." when I press on a button that shows "."
Basically I wanna grab "." on NSString format
So if I do
NSString *dec = [sender currentTitle];
it just crashes when I try to run.
In Cocoa, all controls send notifications that they have been operated, by using a target-action mechanism. The 'target' is any other object and the 'action' is any selector that that object responds to. Buttons are no different.
So you can, for example, define:
-(void)buttonClicked:(id)sender {
NSLog(#"Button was clicked!");
}
You'd hook that up to the button's target-action, by invoking -setTarget: and -setAction: accordingly. The target will be self, if you're doing this from inside the class that handles the action:
[button setTarget:self];
[button setAction:#selector(buttonClicked:)]
Now when the button is pressed, you'll get a NSLog() output in the console.
To update the value of a label instead of printing something with NSLog(), you can probably figure that out, but:
-(void)buttonClicked:(id)sender {
[label setText:#"."];
}
You should read Apple's documentation, which covers this stuff in great detail.
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW14
PS: stackoverflow is for all programming languages, so make sure to tag your questions with the relevant programming language.
Try this.
UIButton *resultButton = (UIButton *)sender;
NSString *dec = resultButton.currentTitle;
Try Something like this
NSString *strdec = [sender titleLabel].text;
Related
I have a simple problem, I have a string like "#my#name#is#umesh#verma" and assign to a UITableview cell label,
cell.detaillabel.text = #"#my#name#is#umesh#verma";
My Problem is how to get each word name when I click on single item.
If I click on #umesh then I get "umesh" word..
More better solution is add custom label which supports touches. For example TTTAttributedLabel supports touches on links.
Main task is get notification when user touch a word and to identify the word.
You can add URLs (with special format) to any word and subscribe to a notification when user click it (as delegate to the label). For example you can create this URL for "word":
touchscheme://word
I haven't checked about how to perform an action when clicking on a UILabel. However, I experienced that with UITextView. You can use "NSLinkAttributeName" to do that.
Basically, from your original string, try to find the range of string that you need to trigger actions. Then add a link value.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:yourString];
[attributedString addAttribute:NSLinkAttributeName value:url range:range];
[textView setAttributedText:attributedString];
Set delegate to your textView and handle the following method
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
// You can retrive your string here or perform an action
return YES;
}
Hope this would be helpful for you.
I've been searching online for this answer, and every single post skips over the part of where to actually write the code for an action. I have a simple Interactive UIButton. And If i could just see a template of code that says "\write code for action here", that would be super helpful!!! ( it's for iPad IOS7 )
This is as far as I can get...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
I think I understand that this is how to set up a potential action, but where do I write the actual code for the action itself?
I want to kind of expand more on what was answered here already, Both responses are correct but i want to explain why/how this all works.
[button addTarget:self
action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
The first thing to look at; The target.
The target is the instance of a class, any class. The only requirement for this class is that it has to implement the action.
action is the method you wish to invoke when the user presses the button.
#selector(aMethod:) Basically think of this as a method signature. Because Objective-c is a dynamic language, aMethod: does not need to exist, but will crash your program if it does not.
So if we put this all together, Whenever I want to press this button:
The system will invoke the action method, on the target instance.
and for the method itself, it can look like this
- (void) aMethod:(id)sender { }
You would put the action related code in a method, in that class, named aMethod:
- (void)aMethod:(id)sender {
// your code for the action goes here
}
You perhaps might also want to use UIControlEventTouchUpInside for the control event.
Since you've set the target as self, the aMethod: method should be added to the same class.
- (IBAction)aMethod:(id)sender
{
// do something here
}
I'm trying to enable a button but the button that I would enable in this function changes. I have an array of the buttons but when I use the .enabled on the array index I want it says that this doesn't work for IDs.
I have used this array to set the text of each button before using:
[[ButtonArray objectAtIndex: Index] setTitle:(#"blahblahblah") forState: UIControlStateNormal];
is there any way to use a similar function call to enable and disable?
If you know for sure that everything in that array is a UIButton you can cast it to make the compiler happy:
[(UIButton *)[ButtonArray objectAtIndex: Index] setTitle:(#"blahblahblah") forState: UIControlStateNormal];
It's OK to split code up into multiple lines. It makes it easier to read, debug, and maintain.
UIButton *button = ButtonArray[Index]; // new, modern array syntax
button.enabled = YES;
[button setTitle:#"blah" forState:UIControlStateNormal];
I must be missing something obvious here but ...
UIControl has a method
- (void)addTarget:(id)target action:(SEL)action forControlEvents: (UIControlEvents)controlEvents
which lets you add an action to be called when any of the given controlEvents occur. ControlEvents are a bitmask of events which tell you if a touch went down, or up inside, or was dragged etc., there's about 16 of them, you or them together and get called when any of them occur.
The selector can have one of the following signatures
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)
none of those tell you what the control event bitmask was. The UIEvent is something slightly different, it's related to the actual touch event and doesn't (I think) contain the UIControlEvent. The sender (UIControl) doesn't have a way to find the control events either.
I'd like to have one method which deals with a number of control events as I have some common code regardless of which event or events happened but I still need to know what the UIControlEvents were for some specific processing.
Am I missing a way to find out what UIControlEvents were used when the action was called or do I really have to separate my code into
- (void)actionWithUIControlEventX;
- (void)actionWithUIControlEventY;
I encountered the same problem, and came up with a solution. It's not amazingly pretty, but it works quite well. It is a UIControl category that stores the last UIControlEvent fired to its own tag property. You can get it from the link below. For further reference, here's the doc from my category, for a more detailed description of what's going on.
Hopefully this helps! Cheers/J
UIControl+CaptureUIControlEvents
git gist at: http://gist.github.com/513796
PROBLEM: upon firing, UIControlEvents are not passed into the target action
assigned to the particular event. This would be useful in order to have only
one action that switches based on the UIControlEvent fired.
SOLUTION: add a way to store the UIControlEvent triggered in the UIEvent.
PROBLEM: But we cannot override private APIs, so:
(WORSE) SOLUTION: have the UIControl store the UIControlEvent last fired.
The UIControl documentation states that:
When a user touches the control in a way that corresponds to one or more
specified events, UIControl sends itself sendActionsForControlEvents:.
This results in UIControl sending the action to UIApplication in a
sendAction:to:from:forEvent: message.
One would think that sendActionsForControlEvents: can be overridden (or
subclassed) to store the flag, but it is not so. It seems that
sendActionsForControlEvents: is mainly there for clients to trigger events
programatically.
Instead, I had to set up a scheme that registers an action for each control
event that one wants to track. I decided not to track all the events (or in
all UIControls) for performance and ease of use.
USAGE EXAMPLE:
On UIControl setup:
UIControlEvents capture = UIControlEventTouchDown;
capture |= UIControlEventTouchDown;
capture |= UIControlEventTouchUpInside;
capture |= UIControlEventTouchUpOutside;
[myControl captureEvents:capture];
[myControl addTarget:self action:#selector(touch:) forControlEvents:capture];
And the target action:
- (void) touch:(UIControl *)sender {
UIColor *color = [UIColor clearColor];
switch (sender.tag) {
case UIControlEventTouchDown: color = [UIColor redColor]; break;
case UIControlEventTouchUpInside: color = [UIColor blueColor]; break;
case UIControlEventTouchUpOutside: color = [UIColor redColor]; break;
}
sender.backgroundColor = color;
}
When you create your UIControl, set a value for the tag property. Then in your action function, you can determine the tag of the UIControl that called it using [sender tag]. Here's an example:
-(void)viewDidLoad {
UIButton *button1 = [[UIButton alloc] initWithFrame(CGRectMake(0.0,0.0,100.0,100.0)];
button1.tag = 42;
[button1 addTarget:self action:#selector(actionWithUIControlEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton *button2 = [[UIButton alloc] initWithFrame(CGRectMake(100.0,0.0,100.0,100.0)];
button2.tag = 43;
[button2 addTarget:self action:#selector(actionWithUIControlEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
-(void)actionWithUIControlEvent:(id)sender {
switch([sender tag]) {
case 42:
//Respond to button 1
break;
case 43:
//Respond to button 2
break;
default:
break;
}
}
-(void)clicketbutton{
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton setTitle:#"Click here" forState:UIControlStateNormal];
[mybutton addTarget:self action:#selector(displayvalue:)forControlEvents:UIControlEventTouchUpInside];
}
-(void)displayvalue:(id)sender{
UIButton *resultebutton= [UIButton buttonWithType:UIButtonTypeCustom];
resultebutton=sender;// pls clear here.. my question here , it it possible or not. if possible how ? NSLog(#" The buttontitile is %# ", [resultebutton.Title] // here also. }
In the above code I create a button and set title as Click here. When I press that button, I want to print Click here, I mean its title. For that my code is here.
iid is the sender, a pointer to the control that's calling your displayvalue method. You're casting the pointer to an integer, and printing the integer result. I'm not sure exactly what you're trying to accomplish, but the fastest way to get an integer from a button to the button's action method is to store it in the tag property.
If you go into a little more detail on what you're working on, I might be able to describe the best way to model that in a Cocoa app. Also, a tip-- be sure to fix any warnings in your code before trying to figure out why something's not working! That id -> int assignment would have made the compiler complain, for example.