How to add a simple UIButton in a UIView by coding?


This post deals with adding a button in UIView by coding…

Create a button

 

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button1.frame = CGRectMake(x, y, width, height);

where x and y are coordinates

width is width of the button,height is height of the button

[button1 setTitle:@"Hello" forState:UIControlStateNormal];

 

Add the button to parent view

[[self view] addSubview:button1];

How to pass a value from one UIViewController to another UIViewController?

This post deals with passing of an integer value from one

UIViewController class to another UIViewController class…

Suppose there are two UIViewController classes

FirstViewController

SecondViewController

 

Now we have to pass an integer value from FirstViewController to SecondViewController

We can use @property to pass values from one class to another but the problem is that the property can be a object only

So we will have to use NSNumber object to store int value as an object and later use it as a property

 

In the SecondViewController.h file

write

NSNumber *value;

@property(nonatomic,retain) NSNumber *value;

In the SecondViewController.m file

@synthesize value; after @implementation

In the FirstViewController.m file

SecondViewController *second=[[SecondViewController alloc] initWithNibName:@”SecondViewController” bundle:nil];
testing.value=[NSNumber numberWithInt:9];

Don’t forget to include SecondViewController.h file in the FirstViewController.h file.

Now in SecondViewController.h file access the value of property by

 

[self.value intValue]