Set title on button according to array amount - objective-c

How do I set the title to each button so it sets the button title to: 1, 2, 3, 4 5 etc. button?
My buttons:
#interface PregnancyViewController () {
NSMutableArray *buttons;
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
for( int i = 0; i < 5; i++ ) {
for( int j = 0; j < 6; j++ ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(j * 50 + 10, i * 50 + 20, 40, 40);
// Add buttons to my mutable array
[buttons addObject: button];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
}

How about this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSUInteger k = 1;
for( NSUInteger i = 0; i < 5; i++ ) {
for( NSUInteger j = 0; j < 6; j++ ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(j * 50 + 10, i * 50 + 20, 40, 40);
NSString* title = [NSString stringWithFormat: #"%#", #(k++)];
[button setTitle: title forState: UIControlStateNormal];
// Add buttons to my mutable array
[buttons addObject: button];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
}

Related

Highlighting the button with sender tag

I have 7 buttons on my storyboard, I have associated a tag number to each of the button. And all the buttons are hooked up to a single IBAction.
In my action method I have a switch statement like switch ([sender tag])
which run the appropriate action according to the tag. This is all working.
But I want to add a functionality where selected button is highlight and rest of them in normal state.
You can create property with tags:
#property (nonatomic, strong) NSArray *tags;
Somewhere (for example, in viewDidLoad) initialize it with values used in storyboard:
tags = #[#1, #2, #3, #4, #5]
And select buttons using this tags
- (IBAction)buttonPressed:(UIButton *)sender {
for (int i = 0; i < tags.count; i++) {
UIButton *button = [self.view viewWithTag:tags[i]];
button.selected = (button.tag == sender.tag);
}
}
Or you can create IBOutlets for every 7 buttons and create array for it.
array = #[outlet1, ..., outlet7]
And select buttons using outlets
- (IBAction)buttonPressed:(UIButton *)sender {
for (int i = 0; i < array.count; i++) {
UIButton *button = array[i];
button.selected = (button.tag == sender.tag);
}
}
Hope this can give you some idea:
- (void)setupButtons {
for (int i = 0; i < 7; i++) {
CGFloat width = self.view.frame.size.width / 7;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * width, 100, width, 30)];
[self.view addSubview:button];
button.tag = 1000 + i;
[button setTitle:[NSString stringWithFormat:#"%d", i] forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
if (i == 0) {
// default first button selected
button.selected = YES;
}
}
}
- (void)buttonClicked:(UIButton *)sender {
for (int i = 0; i < 7; i++) {
UIButton *button = [self.view viewWithTag:(1000+i)];
button.selected = (button.tag == sender.tag);
}
}

Create Grid of UIVews in NSArray

What I am trying to do is align views in a grid that are in an array. I have created the views and the array.
for (int i = 0; i < [directoryContents count]; i++){
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,120)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 100);
button.accessibilityIdentifier = [directoryContents objectAtIndex:i];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 100, 20)];
label.textAlignment = NSTextAlignmentCenter;
label.text = [directoryContents objectAtIndex:i];
[containerView addSubview:button];
[containerView addSubview:label];
[self.view addSubview:containerView];
[_containerViewArray addObject:containerView];
}
[self layoutViews:_containerViewArray inView:self.view];
What I have working is aligning them next to each other the problem I am having is I need to have them wrap down instead of going off the edge. What I am doing to algin them is this
- (void)layoutViews:(NSArray *)views inView:(UIView *)contentView
{
NSInteger overallWidth = 0;
for ( UIView *view in views ) {
overallWidth += view.frame.size.width;
}
CGSize contentSize = contentView.frame.size;
CGFloat startOffset = (contentSize.height - overallWidth)/2.0;
CGFloat offset = startOffset;
for ( UIView *view in views ) {
CGRect frame = view.frame;
frame.origin.x = offset;
view.frame = frame;
offset += view.frame.size.width;
}
}
If we assumed that we have an array of sections (your views) and you need to layout 3 item per row the view has size 98*98 and spacing 6.5px horizontally and 8px Vertically
for (int i = 0; i< [sectionsArray count]; i++) {
int row = (int)i/3;
int col = i%3;
float x = 6.5 + (104.5*col);
float y = 8 + (106*row);
UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(x, y, 98, 98)];
sectionView.backgroundColor = [UIColor clearColor];
//button
UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[sectionButton setTintColor:[UIColor grayColor]];
sectionButton.frame = CGRectMake(0, 0, 98, 98);
sectionButton.tag = i+100;
[sectionButton addTarget:self action:#selector(showSectionDetail:) forControlEvents:UIControlEventTouchUpInside];
[sectionView addSubview:sectionButton];
//adding title
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 75, 98, 20)];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor=[UIColor clearColor];
titleLabel.font=[UIFont fontWithName:#"Helvetica" size:14];
titleLabel.textColor = [UIColor colorWithRed:241.0/255.0 green:124.0/255.0 blue:22.0/255.0 alpha:1.0];
titleLabel.text = [[sectionsArray objectAtIndex:i] objectForKey:#"Title"];
[sectionView addSubview:titleLabel];
[titleLabel release];
[scroll addSubview:sectionView];
[sectionView release];
}
int numberOfRows;
if ([sectionsArray count]/3.0f == 0) {
numberOfRows = [sectionsArray count]/3;
}else{
numberOfRows = [sectionsArray count]/3 +1;
}
scroll setContentSize:CGSizeMake(320, numberOfRows*106);
Here's what I used until the UICollectionView was released, easy to modify to accommodate for how many rows you want in the grid, and the size of the views.
NSUInteger buttonWidth = 100;
NSUInteger buttonHeight = 100;
NSUInteger space = 5;
float scrollContentY = (ceilf((float)imageNames.count / 3.0f) * (buttonHeight + space));
[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
int row = idx / 3;
int column = idx % 3;
UIView *view = [UIView new];
[view setBackgroundColor:[UIColor redColor]];
view.frame = CGRectMake((buttonWidth + space) * column + space, (buttonHeight + space) * row + space , buttonWidth, buttonHeight);
[view setTag:(NSInteger)idx];
[self.scrollView addSubview:view];
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width, scrollContentY)];
}];
Mind you, this might not be advisable if you have a large array to enumerate because this doesn't have any kind of reusable views. It will alloc/init a view for every item in the array.

how to draw Square button in iOS under navigation bar

I have this code and I want to have my button as a square, and also under the navigation bar,would you plase help me
Thanks in advance!
I want to have my button as a square
- (void)viewDidLoad
{
[super viewDidLoad];
int rows = 13, columns = 4;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 70*columns, 70*rows)];
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(70 * x, 28 * y, 70, 28);
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
}
}
// Center the view which contains your buttons
CGPoint centerPoint = buttonView.center;
centerPoint.x = self.view.center.x;
buttonView.center = centerPoint;
[self.view addSubview:buttonView];
}
-(void)buttonPressed:(UIButton *)button
{
NSLog(#"button %u -- frame: %#", button.tag, NSStringFromCGRect(button.frame));
}
Edit 2
my error when I using border
[button.layer setBorderWidth:1.0];
[button.layer setBorderColor:[UIColor blackColor]];
Define an integer variable and set this as button title
int rows = 13, columns = 4;
int number=1;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f,44, 70*columns, 70*rows)];
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(75 * x, 75 * y, 70, 70);
button.backgroundColor=[UIColor redColor];
[button setTitle:[NSString stringWithFormat:#"%d",number] forState:UIControlStateNormal];
button.tag=number;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
number++;
}
}
Hope this will help you to move to next screen. Button.tag gives the exact button you have selected.
You are setting your button of size 70x28, so if you want a button 70x70, you have to change button.frame = CGRectMake(70*x, 70*y, 70, 70), this way you will have square buttons.
Also, are you sure this is a NavigationBar or just a Toolbar? If you use NavigationController which uses navigationBar, Apple's own SDK will not allow you to easily draw over it.
In any case in your case, just start drawing your buttons from y = 44 down and you won't have buttons overlap the toolbar.
Try this
int rows = 13, columns = 4;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f,44, 70*columns, 70*rows)];
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(75 * x, 75 * y, 70, 70);
button.backgroundColor=[UIColor redColor];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
}
}
// Center the view which contains your buttons
CGPoint centerPoint = buttonView.center;
centerPoint.x = self.view.center.x;
buttonView.center = centerPoint;
[self.view addSubview:buttonView];
For using layer property you need to add QuartzCore.framework in your project.

cocoa:Multiple buttons to set the picture,Tab effect

There are several buttons,Click on one of,Change the picture of this button,Other button to set another picture。The following code where there is an error?
NSButton *button = (NSButton *)sender;
NSInteger tag = button.tag;
[button setImage:[NSImage imageNamed:#"menuBtnHover.png"]];
for (int i = 5; i <= 8; i++) {
if (tag != i) {
button =[(NSButton *)[self contentView] viewWithTag:i];
[button setImage:[NSImage imageNamed:#"menuBtn.png"]];
}
}
Add test,button = null
NSLog(#"button tag: %ld %d %#", tag,i ,button);
use this code
NSButton *button = (NSButton *)sender;
NSInteger tag = button.tag;
[button setImage:[NSImage imageNamed:#"menuBtnHover.png"]];
for (int i = 5; i <= 8; i++) {
if (tag != i) { button =[(NSButton *)[self contentView] viewWithTag:i];
if(button)[button setImage:[NSImage imageNamed:#"menuBtn.png"]];}
}

Want to Create buttons inside a UITableViewCell and display as seat layout

I am developing an Theatre Ticket booking iOS Application. In that I want to display a seat layout so that an user can select the number of seats he wants. so i planned to display it in row wise and column wise inside a table view. Is that suggested? Or any other solutions are there?? Here is my code for displaying the buttons inside a UIScroll view. How can I move further. Also I need to handle click events for those buttons and get information and handle later. I need some ideas for that too... Please suggest me some samples.
- (Void) someMethod {
int x = 20, y = 15, rows = [rowsArr count];
for (int i = 0; i < [rowsArr count]; i++) {
NSDictionary * rowsDict = [rowsArr objectAtIndex:i];
NSArray * seatsarr = [rowsDict valueForKey:#"Seats"];
if (![[rowsDict valueForKey:#"RowDescription"] isEqualToString:#"Way"])
[self chapterCreationwithtitle:[NSString stringWithFormat:#"%#", [rowsDict valueForKey:#"RowDescription"]]
withChapterTag:tag + i
andXvalue:x andYvalue:y];
y = y + thumb_incrementY;
if (i == rows) { // (main_scroll.frame.size.width-thumb_incrementX))
x = x + thumb_incrementY;
x = 40;
index1++;
rows = (rows + [rowsArr count]);
}
strSeatDes = [[NSMutableArray alloc] init];
int seats = [seatsarr count];
for (int j = 1; j <= [seatsarr count]; j++) {
NSLog(#"index %d", index2);
NSDictionary * seatsDict = (NSDictionary *)[seatsarr objectAtIndex:j - 1];
[strSeatDes addObject:[NSString stringWithFormat:#"%#", [seatsDict valueForKey:#"SeatDescription"]]];
if (![[seatsDict valueForKey:#"isPassage"] boolValue])
[self seatsCreationwithtitle:[NSString stringWithFormat:#"%#", [seatsDict valueForKey:#"SeatDescription"]] withSeatsTag:tag2 + j andXvalue:xx andYvalue:yy];
xx = xx + thumb_incrementX;
NSLog(#"tag seat:%d", tag2 + j);
if (j == seats) {
yy = yy + thumb_incrementY;
xx = 80;
index2++;
tag2 = tag2 + 100;
seats = (seats + [seatsarr count]);
}
}
NSLog(#"%d", [strSeatDes count]);
main_scroll.contentSize = CGSizeMake(([seatsarr count] + 2) * thumb_incrementX + 110, ([rowsArr count] + 2) * thumb_incrementY);
}
} /* someMethod */
- (void) chapterCreationwithtitle:(NSString *)title withChapterTag:(NSInteger)tags andXvalue:(NSInteger)x andYvalue:(NSInteger)y {
chapter_button = [UIButton buttonWithType:UIButtonTypeCustom];
chapter_button.frame = CGRectMake(x, y, button_width, button_height);
chapter_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[chapter_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[chapter_button setTitle:title forState:UIControlStateNormal];
chapter_button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
[chapter_button setShowsTouchWhenHighlighted:YES];
[chapter_button setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[chapter_button setBackgroundColor:[UIColor darkGrayColor]];
[chapter_button setTag:tags];
// [chapter_button addTarget:self action:NSSelectorFromString(#"getAction:") forControlEvents:UIControlEventTouchUpInside];
[self.main_scroll addSubview:chapter_button];
}
- (void) seatsCreationwithtitle:(NSString *)title withSeatsTag:(NSInteger)tags andXvalue:(NSInteger)x andYvalue:(NSInteger)y {
seats_button = [UIButton buttonWithType:UIButtonTypeCustom];
seats_button.frame = CGRectMake(x, y, button_width, button_height);
seats_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[seats_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[seats_button setTitle:title forState:UIControlStateNormal];
seats_button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
[seats_button setShowsTouchWhenHighlighted:YES];
[seats_button setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[seats_button setBackgroundColor:[UIColor colorWithRed:0.09375 green:0.09765 blue:0.02734 alpha:0.4]];
[seats_button setTag:tags];
[seats_button addTarget:self action:NSSelectorFromString(#"getAction:") forControlEvents:UIControlEventTouchUpInside];
// main_scroll.contentSize = CGSizeMake(x+50, y+50);
[self.main_scroll addSubview:seats_button];
}
If I understand you correctly, you want users to view the seats as they are laid out in the theatre, and to highlight one or more seats in order to book them.
If this was me, I would not use a UITableView. I think in iOS tables are actually more for displaying lists than two dimensional information. However, it can be done - basically you make your own custom cell which is split in to columns. Some code here https://github.com/AlanQuatermain/AQGridView might be useful.
Personally I would think about maybe just having a grid of UIButton controls instead. It depends on how many seats there are, and if they fit on to a single screen.