Programmatically create UITextFields - objective-c

I can't figure out what is wrong here. Please see coments above NSLog function.
-(void)loadView
{
......
int x_position = 10;
for (self.x = 0; self.x < 3; self.x++)
{
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, x_position, 300, 25)];
self.textField.tag = self.x;
// Output 0, 1, 2
NSLog(#"%d", self.x);
x_position += 40;
[self.view addSubview:self.textField];
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self action:#selector(showNames) forControlEvents:UIControlEventTouchDown];
[btn setTitle:#"Remove from view" forState:UIControlStateNormal];
btn.frame = CGRectMake(0, x_position + 30, 210, 50);
[self.view addSubview:btn];
[self.textField release];
[self.view release];
}
-(void)showNames
{
while (self.x > 0) {
self.x--;
// output 2, 1, 0
NSLog(#"%d", self.x);
NSLog(#"%#", tmp);
}
}
Here is console log
<UITextField: 0x4b39410; frame = (10 90; 300 25); text = 'Ad'; clipsToBounds = YES; opaque = NO; tag = 2; layer = <CALayer: 0x4b38c30>>
<UITextField: 0x4e22320; frame = (10 50; 300 25); text = 'Asd'; clipsToBounds = YES; opaque = NO; tag = 1; layer = <CALayer: 0x4e0a4c0>>
<UIView: 0x4b32330; frame = (0 20; 320 460); layer = <CALayer: 0x4b329a0>>
I expect object at tag 0 to be UITextField, not UIView.
What is wrong here ?

Every view's tag defaults to zero, so your main UIView will have a tag of zero, and so will any other view where you didn't explicitly set the tag.
I suggest using an offset value for your tags, so you can make them all unique. For example:
#define TEXTFIELD_TAG_OFFSET 100
for (self.x = 0; self.x < 3; self.x++)
{
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(10, x_position, 300, 25)];
self.textField.tag = self.x + TEXTFIELD_TAG_OFFSET;
// Output 0, 1, 2
NSLog(#"%d", self.x);
x_position += 40;
[self.view addSubview:self.textField];
}
Now you can reference the Nth textfield with the tag number TEXTFIELD_TAG_OFFSET + N. That way your textfields will all have unique tags.

Related

objective-C loop a matrix world

I want to use label to create a 15 * 15 matrix world.How to loop the row and column?
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 15, 15)];
myLabel.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:myLabel];
Assuming you need to create 50 Label of size 18x98
int x = 0;
int y = 0;
for (int i = 0; i < 50; i++) {
if (20 * x > self.view.frame.size.width) {
x = 0;
y++;
}
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * x, 100 * y, 18, 98)];
myLabel.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:myLabel];
x++;
}

Set title on button according to array amount

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];
}
}
}

GCD Scrollview loading issue

I'm using GCD to load images for my scroll view but rather than following the frames i created it makes only a single frame in which the images just flicker through. Here's my code:
//Create the array
-(void)awakeFromNib {
images = [NSMutableArray arrayWithObjects: [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"imageOne" ofType:#"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"imageTwo" ofType:#"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"imageThree" ofType:#"png"]],nil];
}
//Populate scrollview
-(void)populate {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
scrollview.pagingEnabled = YES;
scrollview.backgroundColor = [UIColor clearColor];
scrollview.clipsToBounds = YES;
scrollview.delegate = self;
[scrollview setContentSize:CGSizeMake(2 * scrollview.frame.size.width, scrollview.frame.size.height)];
int number = [images count] ;
NSLog(#"%i",images);
int rowTwoInt = 0;
int rowThreeInt = 0;
int rowFourInt = 0;
for (int i = 0; i < number; i++) {
if (i <= 3) {
finalFrame = CGRectMake( i*(80), 12, 80, 80);
}
if (i == 4) {
finalFrame = CGRectMake(0, 95, 80, 80);
}
if ((i >4) && (i <= 7)) {
rowTwoInt = rowTwoInt + 80;
finalFrame = CGRectMake(rowTwoInt, 95, 80, 80);
}
if (i == 8) {
finalFrame = CGRectMake(0, 178, 80, 80);
}
if ((i > 8) && (i <= 11)) {
rowThreeInt = rowThreeInt + 80;
finalFrame = CGRectMake(rowThreeInt, 178, 80, 80);
}
if (i == 12) {
finalFrame = CGRectMake(0, 261, 80, 80);
}
if ((i > 12) && (i <= 15)) {
rowFourInt = rowFourInt + 80;
finalFrame = CGRectMake(rowFourInt, 261, 80, 80);
}
IconButton = [UIButton buttonWithType:UIButtonTypeCustom];
IconButton.frame = CGRectMake(0, 0, 57, 57);
IconButton.center = CGPointMake(40, 29);
[IconButton addTarget:self action:#selector(Selected:) forControlEvents:UIControlEventTouchUpInside];
IconButton.tag = i;
dispatch_async(queue, ^{
UIImage *icon = [images objectAtIndex:i];
dispatch_sync(dispatch_get_main_queue(), ^{
[IconButton setImage:icon forState:UIControlStateNormal];
});
});
[cell addSubview:IconButton];
cell = [[UIView alloc] initWithFrame:finalFrame];
[scrollview addSubview:cell];
So what am i doing wrong? I'm new with GCD so it's probably something with that. Also if you see a way to improve UIScrollView performance, please let me know.
I suspect you have declared IconButton as an instance variable, a static variable, or a global variable.
Each time through your loop, you change the value of IconButton. By the time your twelve queue blocks execute, IconButton is set to its final value - a reference to the last button you created. All the blocks use the instance (or static or global) IconButton variable, so all the blocks update the same, final button.
Make a local variable to reference the button, and use that local variable in the loop:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
IconButton = button;
IconButton.frame = CGRectMake(0, 0, 57, 57);
...
dispatch_async(queue, ^{
UIImage *icon = [images objectAtIndex:i];
dispatch_sync(dispatch_get_main_queue(), ^{
[button setImage:icon forState:UIControlStateNormal];
});
});
Each block will capture the value of the local variable at the moment the block is created, so each block will capture a reference to a different button.

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.