Creating The View And Adding Sub-view Progmmatically


      Creating The View  And Adding SubView  Progmmatically

Here in our project i  created a view manually in that we are going the to Add button, 2 labels and another View Controller.

Process:
             The entire output will look like, selecting a gender from dropdown box.
             When ever user clicks on the button to select the gender. 
             One subview will appear, in that subview the user has to pick up related element on selecting it.

Step 1:

First of all we need to declare some objects as instance variables in order to get availability

->@interface ViewController : UIViewController
{
//two labels
    UILabel *lbl,*lbl1;
//main view and sub view
    UIView *vi,*vi1;
//nsarray object to add elements
    NSArray *arr;
//uyitable view
    UITableView *tbl;
}
//action to perform when button is clicked
-(void)click:(id)sender;
@end

Step 2:

->add elements to array object 

 arr=[[NSArray alloc]initWithObjects:@"male",@"female",@"othere", nil];

 Creating view 
->in order to create a view we are having a class called UIVIEW
->create object for that view and give the necessary mesurements to the view, the default resolution of the iphone is 640X960 

UIView *vi=[[UIView alloc]initWithFrame:CGRectMake(0,0,640,960)];

->set background color of the view to white

    vi.backgroundColor=[UIColor whiteColor];

Step 3:

->Now add button to the view, to create a button we are having a class called  UIBUTTON
->Create an object to the ui button and give the positions and size of the button.
->Inorder to perform any operations when ever the button is pressed we have to give action as @operator(action name).

->And give control events as touch up inside

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(200,135,72,37)];
    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

->Set title and background to the button

    [btn setTitle:@"Gender" forState:normal];
    btn.backgroundColor=[UIColor grayColor];

Step 4:

->Now add two label to display the names on the screen
->To create the label we are having the class called UILABEL class.

lbl=[[UILabel alloc]initWithFrame:CGRectMake(25,135,72,37)];
    [lbl setText:@"Gender :"];
    lbl1=[[UILabel alloc]initWithFrame:CGRectMake(100,135,72,37)];
    [lbl1 setText:@" Press"];

Step 5:

->Now add button and labels to the view controller

    [vi addSubview:btn];
    [vi addSubview:lbl];
    [vi addSubview:lbl1];

Step 6:

->Make the created viewcontroller as default viewcontroller
self.view=vi;

Step 7:

->Implement the action of the button
-(void)click:(id)sender
{
}
->In this Action we need to display the elements in the another view
-> So to display the elements in the sub view, add table view to the sub view controller
    //Creating the sub View
    vi1 =[[UIView alloc]initWithFrame:CGRectMake(200,165,100,150)];
   //Adding background color to the sub View
    vi1.backgroundColor=[UIColor grayColor];
   //Creating table 
    tbl=[[UITableView alloc]initWithFrame:CGRectMake(00, 0, 100, 150) style:UITableViewStyleGrouped];
   //Setting Delegates and datasource to the table view
    [tbl setDelegate:self];
    [tbl setDataSource:self];
   //Adding table View to the Sub view
       [vi1 addSubview:tbl];

Step 8:

   //Adding SubView to the main ViewController
    [vi addSubview:vi1];

Step 9:

->To display the elements in the table we need to implement the delegate methods
->In that two methods are required methods
->And other methods implementation is done according to our requirement

 Required Methods:

//in this method we need to return the cell count of  the TableView
//here we passed nsarray length as cell count.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arr count];
}

//in this method we need to fill data in cell and return the cell object
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//here we need to create string inorder to identify the cell
    NSString *cell1=@"identity";
//inorder to avoid creating of unwanted cell objects we need to reuse the created cell 
    UITableViewCell *cell=[tbl dequeueReusableCellWithIdentifier:cell1];
//here it will check whether the cell is existing or not
    if(cell==nil)
    {
//if cell is not existed it enters the loop and create object to the cell 
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell1];
    }
//here we are filling the data in the cell from array according to the index of the cell
    cell.textLabel.text=[arr objectAtIndex:indexPath.row];
//we are returning the uitableviewcell object
    return  cell;    
}

Step 10:

->To make selection  in the table cell we are using this method
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//here it will print the selected value in the label on getting the index path of the cell. 
    lbl1.text=[arr objectAtIndex:indexPath.row];
   //after completing all the process we are hiding the subview
    vi1.hidden=YES;
     return nil;
}











tags:how to create a subview in iphone,creating subview iphone,addsubview in view,how to get subviews of uiview,how to get subviews of uiview, iphone sample projects, sample programs in iphone, sample apps for iphone with source code

2 comments:

vasantharao said...


i got maximum befitted from your instruction of obective-c

i am expecting more things about ios

Thank u...
vasanthsrichakra

vasantharao said...

I like much this site bez u covered mazor things of the iPhone


Thank You
Mallibusty

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates