I added 10 buttons to a view (example view name is "menuView"), now I want to remove the background image for 2nd, 3rd, 4th buttons. I wrote the code like this
for(id btn in [menuView subViews]){
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
The problem with this code is, it is removing all 10 button's backGroundimage, but I need to set nil for the 2nd, 3rd, and 4th buttons
If you create a tag for the buttons that you add, you can filter against them.
for(UIButton *btn in [menuView subViews]){
if (btn.tag == 2 || btn.tag == 3 || btn.tag == 4) {
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
}
Of course, you need to make sure that there are no other views in menuView that could share the same tag. So the choices are to make the tags large, unique values, or checking that they are actually UIButtons. I've edited this assuming that the only subviews of menuView are UIButtons. Enumerating over UIButtons won't cause compiler warnings about tag not being a property of NSObject.
UIButton is a subclass of UIControl which is a subclass of UIView. UIView has the tag property, so UIButton inherits this property. It's useful to look at the docs for a class that you are using, and continue up the hierarchy to see if there are properties or methods that are useful for what you need to do.
Just to expand on my comment.
Using an IBOutletCollection which you can point an array to many objects in the nib. You declare this as such (synthesizing in the implementation):
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *threeButtons;
This says to IB that it's a collection of UIButton elements. In IB, you connect this to the three buttons you wish to remove the background image for, by control dragging from it to the buttons. Once this is done the array will contain those buttons you connected up and you can loop like so:
for (UIButton *button in self.threeButtons) {
[button setBackgroundImage:nil forState:UIControlStateNormal];
}
Again, the link to a more detailed explanation can be found at: http://bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection
When creating the buttons, try using the "tag" property. Then, when you're setting the background to nil, you could check for btn.tag == 2,btn.tag == 3 or btn.tag == 4.
You could have assigned tags to the buttons from 1 to 10 when adding them to menu view. And now with the help of tags, we can decided what to do with buttons.
Firstly, are you placing the buttons using Interface Builder?
If so, I'd recommend placing numbered tags for each of the buttons and then you can use something like the following to find the appropriate buttons and remove the background image.
for(UIButton *buttonname in [yourView subViews]){
if (buttonname.tag == 2 || buttonname.tag == 3 || buttonname.tag == 4) {
[buttonname setBackgroundImage:nil forState:UIControlStateNormal];
}
}
If you're creating them programmatically and sequentially, I'd recommend placing the buttons in an array as they're made and just remove the background of the buttons using "objectAtIndex".
Related
I want to add a sound to every button sub-view in my UIView. I have so many of these buttons so I dont want to make a category or subclass of UIButton because in that case I have to change class name everywhere.
Is there anyway that I just loop [currentView subViews] and get button one by one, and add an action method.
Please keep in mind that buttons already have actions as well. So i am in search of some way to add definition to that action selectors.
try something like below...its not tested but should work...
for(UIButton *button in currentView.subViews) {
if ([button isKindOfClass:[UIButton class]]) {
[button addTarget:self
action:#selector(playSound)
forControlEvents:UIControlEventTouchUpInside];
}
}
I need to implement a grid view of images that requires each segment to segue to another view controller /or View.
My parameters:
I need 260 segments: approx 18px x 18px
Each segment will be numbered (1-260) and have a diffrent (background) image
One segment must be highlighted (a daily square like ical)
You can tap any segment to segue to the next view controller
I have looked at custom TVCells / Buttons but are there more options out there?
Thank you
In the end I opted for:
Linking the 1stViewController to the 2ndViewController in the
storyboard and naming the seque.identifier in the attributes.
Creating a grid of UIButtons in IB and assigning them individual tags in the attributes (1-260 - don't use 0 as it is a default).
To change the buttons' backgroundImage daily I set up a day-counter integer
and in the viewDidLoad of the 1stVC coded:
[(UIButton*)[self.view viewWithTag:dayCount] setBackgroundImage:[UIImage imageNamed:#"image_Day.png"] forState:UIControlStateNormal];
As there were multiple UIButtons, I decided that dragging them all to a IBAction in IB was too long a task so I assinged them programatically using:
-(void) assignButtons{
[(UIButton*)[self.view viewWithTag:1] addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[(UIButton*)[self.view viewWithTag:2] addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
} //etc for all 260
Then used performSegueWithIdentifier: in a method:
-(IBAction) buttonClicked:(id)sender{
[self performSegueWithIdentifier:#"mySegue" sender:self];
}
I need to implement a single button which would be shown at upper right corner of the application on UIView or MKMapView. On clicking that button a combo should come up and user would be able to select the categories.
How can I achieve that?
You have to create an UIButton and add it as a subview of your UIView (for example in viewDidLoad method if your view is linked to an UIViewController).
UIButton *showButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showButton.frame = CGRectMake(500, 20, 150, 44); // hardcoded frame, not quite elegant but works if you know the dimension of your superview
[showButton setTitle:#"Show Categories" forState:UIControlStateNormal];
// add target and actions
[showButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a superview, your parent view
[superView addSubview:showButton];
Then you add a method, called buttonClicked: that takes an id parameter (usually the sender, showButton in this case).
-(void)buttonClicked:(id)sender
{
// visualize categories
}
To visualize the categories you can follow two different ways:
Present a UITableViewController inside a UIPopoverController (only for iPad device)
Show a modal controller presenting a UITableViewController (both iPad and iPhone devices).
The UITableViewController allows you to have a list of categories and then select one of them.
P.S. Check the code in XCode because I've written by hand (without XCode)
i need to place menu button in iphone.
i have some touch with android.
In android i display like this by pressing menu.
Is it possible to handle iphone menu button.
if it is not where can i place these.
Thank u in advance.
use the round rect button and change the property into custom place the image as the background image of the button do this for more menu item in your home page.
this is the way i do the menu item.......
I think with Xcode and Interface Builder (iPhone's development platform), getting this task done would be even simpler.
As a brief introduction to iPhone app's UI design, to place this tab bar (as you put it, the menu) on a view, all you need to do is pick up a tab bar from the library and place it on a nib file's view. You can also connect such a tab bar with some codes responsible for internal logic by simply clicking and dragging. It's simple.
btw, a nib file is just some file format Xcode and Interface Builder use to hold the UI data.
iPhones don't have a dedicated slide out menu - everything is on the screen at all times, and there are no purpose built buttons (like back and home).
You should either always show your actions on the screen, or if they are unimportant or infrequent, hide them behind another action button (like a + or wrench icon).
You should use images same as menu items and put custom button over the images.This is the only way to show menu as you want.
Iphone sdk provides tabBar and toolBar, you also can use these in customize forms.This is the correct way to making menu.Iphone design pattern having lot of strengths over android so you can make every thing in Iphone easily but you cant make every thing in android same as iPhone.Iphone having international standards so must use iPhone's controls.
.h
IBOutlet UIScrollView *scrollView;
#property ( nonatomic , retain ) IBOutlet UIScrollView *scrollView;
-(void)AppleVijayAtFacebookDotCom:(id)sender;
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons;
.m
#synthesize scrollView;
-(void)AppleVijayAtFacebookDotCom:(id)sender{
NSLog(#"AppleVijayAtFacebookDotCom called");
UIButton *button=(UIButton *)sender;
if (button.tag == 0) {
NSLog(#"hey have clicked first button, this is my tag : %i \n\n",button.tag);
}
else if (button.tag == 1) {
NSLog(#"hey have clicked second button, this is my tag : %i \n\n",button.tag);
}
// ......like this
NSLog(#"button clicked is : %iBut \n\n",button.tag);
}
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons{
for (int i = 0; i < totalNoOfButtons; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(AppleVijayAtFacebookDotCom:) forControlEvents:UIControlEventTouchUpInside];
//[button1 setImage:[UIImage imageNamed:#"Button.png"] forState:UIControlStateNormal];//with image
//OR
[button setTitle:[NSString stringWithFormat:#"%iBut",i] forState:UIControlStateNormal];//with title
button.frame = CGRectMake(i*(offset+buttonSize.width), 8.0, buttonSize.width, buttonSize.height);
button.clipsToBounds = YES;
button.showsTouchWhenHighlighted=YES;
button.layer.cornerRadius = 10;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.backgroundColor=[UIColor blackColor].CGColor;
button.layer.borderWidth=2.0f;
button.tag=i;
[self.scrollView addSubview:button];
}
self.scrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
//self.navigationItem.titleView=self.scrollView;//if u have navigationcontroller then enable this line
}
Dont forget to connect the scrollView in interface builder
while creating the scrollview in IB make sure ur scrollView height is 44.which is default to navigation bar.so it will look nice.
in viewDidLoad call
[self createMenuWithButtonSize:CGSizeMake(70.0, 30.0) withOffset:20.0f noOfButtons:30];
OUTPUT
Trying to do simple button functionality.
Button A. It has 3 states: default, highlighted, selected. Should UNSELECT when clicked again.
For the life of me, I can't even get a simple 3 state functionality established.
Highlight (on press) appears built in (goes to "stock" blue).
I"ve used Button Attributes to load in image for selected state...PLayed with Control/Content to click Highlight and Selected on and off, trying to find the right combo...
I thought it'd simply be selecting in dropdown the state I want to edit....and it would register my edits for that state...images loaded/ colors changed/ etc....
NO!!!!
What am I missing..?
What are you trying to do set the images or titles for the different states? For that you can just use methods such as
-(void)setImage:(UIImage *)image forState:(UIControlState)state;
OR
-(void)setTitle:(NSString *)title forState:(UIControlState)state;
is that what you are asking. May be i am misunderstanding your question because its not clear enough.
I couldn't find a straight answer about this anywhere. So I brewed my own solution that seems to work great.
Dont forget to wire up your buttons in IB you will need
to bind both
touch up inside to setButtonPressed
file owner to button ex. 1stButton
Psuedo Code
Clear all buttons selected
Set sender to selected
CODE
//Declare your buttons in .h
UIButton *1stButton;
UIButton *2ndButton;
UIButton *3rdButton;
#property(nonatomic, retain) IBOutlet UIButton *1stButton;
#property(nonatomic, retain) IBOutlet UIButton *2ndButton;
#property(nonatomic, retain) IBOutlet UIButton *3rdButton;
//In .m file - write 2 methods
-(void)clearButtons
{
[1stButton setSelected:FALSE];
[2ndButton setSelected:FALSE];
[3rdButton setSelected:FALSE];
}
//attach to touch up inside event in IB for each button
-(void) setButtonPressed:(UIButton *)sender
{
self.clearButtons;
[sender setSelected:TRUE];
[sender setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
}
You really should post some code (or an IB screenshot) as it's hard to fully parse what you are saying.
The default behavior of a UIButton is to show the Selected state on press, then revert back to the normal state. If you want it to "lock down" a press, you need to do something like this in the IBAction hooked to the UIButton:
BOOL pressed;
UIButton *button
- (IBAction) toggleButton:(id)sender
{
button.selected = ! pressed;
pressed = ! pressed;
}
AH! Well, not being a programmer, I was hoping for simple "stock" solution within Interface Builder.
To be clear of the "want/wish:
Button A-E. Each button should have 3 states: Default, highlight, selected.
Default: resting state.
Highlight: some treatment using interface builder features or import a graphic that will appear on Highlight. This state appears while button is being pressed.
Selected: when button A-E is released, it shows that it has been selected by displaying a new state, again by using tweaks within interface builder (font color, shadow) or import a graphic. When you click the SELECTED button again, it returns to Default state.
I guess I just thought it'd be "easy" because SDK 3.0 Interface Builder has dropdowns for the 3 states. I thought code would "magically" be assigned to the button allowing it to function as desired along with it's aesthetic shift.
I TOTALLY appreciate any and all feedback. I can pass along any coding advice to partner on this App.
Try this:
UIImage *img1 = [UIImage imageNamed:#"image1.png"];
UIImage *img2 = [UIImage imageNamed:#"image2.png"];
UIImage *img3 = [UIImage imageNamed:#"image3.png"];
[button setImage:img1 forState:UIControlStateNormal];
[button setImage:img2 forState:UIControlStateHighlighted];
[button setImage:img3 forState:UIControlStateSelected];
[button setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];
[img1 release];
[img2 release];
[img3 release];
This is because the state variable is actually a bit flag.