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;
}
}
Related
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)
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
}
}
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
I have this working in C#, but don't know where to go with Objective C (Xcode specifically).
I have 8 buttons
Stack1, Stack2, etc.
I want to choose Stack(variable) and change the image.
In C# I used
Button Stacks = this.Controls["Stack" + StackNumber.ToString()] as Button;
Stacks.BackgroundImage = .....
Can I do this in Objective C also?
I hope I understand your question correctly. You want to change button's image by clicking on another button. You can assign tag property to your different buttons like stack1.tag = 1001; , stack2.tag = 1002; , stack3.tag = 1003;
Now in your button click method :
- (IBAction)buttonStack1Click:(id)sender // for stack1 button
{
UIButton *tempStack = (UIButton *)[self.view viewWithTag:1003]; //to change image of stack3 button
// Now change the image of stack3 button
[tempStack setImage:[UIImage imageNamed:#"su.png"] forState:UIControlStateNormal];
}
Hope it gives an idea...
Take help of for loop and implement the code below :
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];
Any query arises,you can tell me.. hope that helps you.
If you want to perform certain function when a button is clicked then there are two ways..
One is Sarah way:Creating Button programmatically
The Other ways is if you are designing buttons in your interface builder :
Then :
1) Make those Buttons an IBOutlet..Also Make IBAction(Lots of tutorials available online..google them)
2) The IBAction will be something like this : - (IBAction)StackButton(YourNumber)Clicked:(id)sender {
In that function change the image of the button you want.
Like
[StackButton1 setImage:MyImage forState: UIControlStateNormal];
I wanted to have a button appear in a table view as the first element on the last row instead of the normal data, so I created a subclass of NSTableView and overwrote the preparedCellAtColumn:row: method to generate the desired behaviour on the last row.
- (NSCell *)preparedCellAtColumn:(NSInteger)column row:(NSInteger)row {
if(row < [self numberOfRows]-1) {
return [super preparedCellAtColumn:column row:row];
} else {
// if we're on the last row, we're going to add the button instead of data
if(column == 0) {
NSButtonCell *button = [[NSButtonCell alloc] initTextCell:self.buttonTitle];
[button setEditable:YES];
[button setSelectable:YES];
[button setBezelStyle:NSTexturedSquareBezelStyle];
[button setGradientType:NSGradientConvexWeak];
[button setButtonType:NSMomentaryPushInButton];
[button setHighlightsBy:NSBackgroundStyleLowered];
[button setAction:#selector(openAddDialog)];
[button setControlView:self];
return button;
} else {
NSTextFieldCell *emptyCell = [[NSTextFieldCell alloc] initTextCell:#""];
[emptyCell setEditable:NO];
[emptyCell setSelectable:NO];
return emptyCell;
}
}
}
Fortunately, the functionality works--the button appears on the last row, and it calls the openAddDialog selector when it is clicked. There are two visual problems, however:
1. The button doesn't highlight (press down) when it's clicked.
2. The table selects the row with the button on it when it is clicked.
I'm still pretty new to Objective C and Cocoa, so I'm not really sure where to look for the solutions to these. Why isn't the button's setHighlighted method getting called when it is clicked? What method is called just before the NSTableView selects a row so I can override it and tell it not to select the last row?
I ended up solving my second question by overriding the selectRowIndexes:byExtendingSelection: function in my table subclass and simply not passing the last row index on to the super function.
I'm pretty sure you have to make the column editable in order to activate a UI element inside of it.
BTW, it's usually a bad idea to put some kind of control at the end of a table, especially one that affects the entire table. Tables should be composed of logical repeating units. Putting something unique in a table causes user confusion and can hide functionality. You most likely want to move the button outside the table itself.