When I click on a UIButton it should return a value I assigned - uibutton

How to assign a value to UIButton so that when I click it, it should return me that value.
For EX : if i have 10 buttons and when i click first button it should return me 01 and when i click second button it should return me 02 and so on.

Set tag value for each button and access the value from button action like buttonname.tag.
Set tag value :
And get the tag value from action of the button like this
- (IBAction)doMyAction:(UIButton *)sender {
NSLog(#"Selected Button %d",sender.tag);
}
if your creating programmatically do like this :
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(0,100,50,50)];
button1.tag = 01;
And retrieve as same above.

Related

Copy Text from UITextView inside a UITableView

I have a tableview cell that has an image view, a UILabel and a TextView. I want the user to be able to select text from the TextView when they longPress on the TextView.
I have created a Gesture recogniser for the cell, and when the user long presses on the cell, the gesture recogniser gets called, but I don't get a menu or a cursor for the user to be able to select text and then copy it. Below is the code to attach long press to the UITableView cell
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LongPressgesture:)];
[tableView addGestureRecognizer:longPressRecognizer];
I also have the following code that gets called
- (void)LongPressgesture:(UILongPressGestureRecognizer *)gesture{
if (gesture.state == UIGestureRecognizerStateEnded) {
NSLog(#"Long press Ended .................");
}
else {
NSLog(#"Long press detected .....................");
}
}
The function gets called, so I see the message "Long press Detected" on the output log, but I don't get the menu to copy text. I tried creating a menu on the Long Press section as this:
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:#"Copy" action:#selector(copyText:)];
UIMenuController *menuCont = [UIMenuController sharedMenuController];
[menuCont setTargetRect:CGRectMake(10, 100, 400, 400) inView:tableview];
menuCont.arrowDirection = UIMenuControllerArrowDown;
menuCont.menuItems = [NSArray arrayWithObject:menuItem];
[menuCont setMenuVisible:YES animated:YES];
But the copy text function is not called, and there is no menu.
Thanks!

iOS Display Different Image on Click

Using XCode, I am trying to figure out how to display a different image when someone clicks or presses down on one of my buttons before being taken to a second screen. For example, I have a contact icon on my home screen. When a user clicks the icon, it should change to a darker version on tap before going to the contact screen. Any help is appreciated.
-(IBAction) ButtonPressed :(id)sender
{
UIButton *tempButton = (UIButton *) sender;
int tag = tempButton.tag;
NSString *viewName;
switch (tag)
{
case 1:
[FlurryAnalytics logEvent:#"Contact-Screen"];
viewName = #"ContactScreen";
if( self.appDelegate.sound)
[Click play];
[self.appDelegate moveToView:viewName];
break;
}
}
Simple, you just have to set a different image for the buttons selected state:
[myButton setImage:[UIImage imageNamed:#"mySelectedImage"] forState:UIControlStateSelected];
Or in interface builder:
Additional UIControlState's include:
enum {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0,
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2,
UIControlStateApplication = 0x00FF0000,
UIControlStateReserved = 0xFF000000
};
The state of a control; a control can have more than one state at a
time. States are recognized differently depending on the control. For
example, a UIButton instance may be configured (using the
setImage:forState: method) to display one image when it is in its
normal state and a different image when it is highlighted.
As far as I've ever noticed in Xcode, whenever you decide to implement a customized button and don't specify a second image for use in the UIControlStateSelected state, Xcode automatically darkens the button for you when the user taps the buttons to show that they were, in fact, tapped.
The 0x7f answer is almost correct (the correct state value is 'Highlighted' and not 'Selected'). For Swift users, it would be
myButton.setBackgroundImage(UIImage(named: "myButtonImagePressed.png"), forState: .Highlighted)

iOS how to identify which image (button) is pressed?

I've three randomly generated images( actually UIButton) and when application runs it asks the user to choose one random image among the three(say a.png).after user select the image app will do some thing based on correct image was selected or not.
Now the question is that how can i identify that user select right image? I'm trying to get name of image that user select and then check it, but really no idea how to do that.
I've searched google for this but can't find something useful.
Can anyone help?
Thanks
When you really want to store the name of the image into the button, instead of using the tagproperty, you could set it to the title and hide the titleLabel (apple reference says: Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance of the button label.):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
button.titleLabel.hidden = true;
[button setTitle:#"a.png" forState:UIControlStateNormal];
then you can access to the imagename with button.currentTitle.
While generating your UIButtons you can fill the tag property.
Then you can check if your sender.tag equals the value you filled in.
When you only want to check if the correct image is selected, you can set only this one to 1 and the other buttons to 0.
To know which button clicked u need to add tag say from 0, 1, 2
[yourButton setTag:0]; //set tag 1 and 2 also to other button
Now add same selector or method to all buttons:
[yourButton setTarget:#selector(buttonClicked:) forState:UIControlTouchUPInside];
// setTarget to other buttons too
method would be this:
-(void)buttonClicked:(id)sender
{
if([sender tag] == 0)
{
//button 1 clicked
}
else if([sender tag] == 1)
{
//button 2 clicked
}
else if([sender tag] == 2)
{
//button 3 clicked
}
}
Well if you are using UIButtons for this purpose, you could set Target Selector on each button and determine the UIButton using their tag values.
There's also one more method to help. But only of you use 'backgroundImage'.This action is should be linked with your buttons:
-(IBAction)photoIconClicked:(id)sender{
if ([sender isKindOfClass:[UIButton class]]){
UIButton *button = (UIButton*)sender;
_currentImage = button.currentBackgroundImage;
}
}

UIButton action not working

I am having some trouble with the UIButton.
This is my code:
I am not using the Interface Builder and I am a new programmer.
What I want is that when the button is selected the title changes and the button's transparency changes from half visible to fully visible.
Thanks in advance, -Marnix
The problem is that the if statement is just executed once, when you're creating the button. Inside MyCode2, add this line:
[Button1 addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
Every time that the button's pressed, the buttonAction method will be executed, so:
- (IBAction)buttonAction:(id)sender {
if(Button1.selected == NO) {
// Do your "NO" stuff
}else if(Button1.selected == YES) {
// Do your "YES" stuff
}
}
You have to declare this IBAction in your .h and add this method in your .m file.
First of all, you should set title for different control state like
[Button1 setTitle:#"UnTapped" forState:UIControlStateSelected];
Second, you don't need to put setTitle method inside of this check.
Besides, you need to put this check inside of a method of the button to make it work. If you didn't use IB, then just use addTarget: action: forControlEvent like
[Button1 addTarget:self actoin:#selector(changeButtonState:) forControlEvent:UIControlEventTouchUpInside];
Lastly, did you make the button keep selected after touch? If not, add
Button1.selected = !Button1.selected
in the method. All in all, your method should looks like this
- (IBAction)buttonAction:(id)sender {
Button1.selected = !Button1.selected
if(Button1.selected == NO) {
Button1.alpha = 0.5
}else if(Button1.selected == YES) {
Button1.alpha = 1.0
}
}

Best way to create dynamic list of links/buttons in iOS5 view controller

I want to create a dynamic set of links/buttons in an iOS5 view controller and am trying to figure out what might be the best way to do this.
For eg:
Item 1
Item 2
Item 3
:
:
Item N
Each of the Items is a link/button that is clickable and will do some action, like load another screen etc based on the link.
I don't know ahead of time how many items there might be so if all the items don't fit on the screen, I need to be able to scroll to view.
My question:
1. What is a better way of doing this? I could just create the labels and buttons dynamically but this seems rather cumbersome and I'm not entirely sure how I would differentiate between the different buttons (essentially I'd need some index to find out which Item was clicked).
2. Alternatively, I was wondering if I can just render this page as HTML and just have links? I've never done this and not sure how I'd associate a button with a link.
Any suggestions?
AK
You can try to use the tag property to store the index value you need when you create the button. Then evaluate it in the button tap handler by accessing using button.tag.
Maybe you can try Cordova for an HTML based approach. I'm not too familiar with it though, so I can't say for sure.
Hope it helps.
(1) You can assign UIButton tag property based on the button index. If any events were to trigger, you could recognize which button the event belongs to by checking the tag.
Sample :
// Initializing some buttons
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.tag = 1;
[button1 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.tag = 2;
[button2 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
Selector for button events :
- (void)buttonPushed:(id)sender {
...
if ([sender tag] == 1) {
// do something after button1 event
} else if () {
// do something after button2 event
}
...
}
(2) If you choose to do it in HTML, you could check out CMHTMLView