Posted By Our Mentors
Amreen Khan
Android Developer
animesh bansal
iphone App Developer
Devendra chouksey
Android Developer
Dilip Saket
IOS apps developer
Hemraj Jhariya
iOS Developer
Jaihind singh Rajput
Smart Phone App Dev
Mujahid khan
Android developer
Narendra Yadav
android developer
Narinder Gupta
Mobile App Architect
Pradeep singh Thakur
Mobile App Architect
Sanjay Sharma
Andriod Developer
Shakti Rajpal
IT Architect
Soniya Vishwakarma
iPhone App Developer
Sunil Raghuwanshi
iPhone App Developer
Supriya Kapse
Senior developer
View current Topics
Opening different Views on each Segment of SegmentControl Iphone/iPad
This tutorial will help in those situation where you need to open different Views on each segment of Segment Control
Posted By: Soniya Vishwakarma Date: 26 Jun 2012 View: 2336
      Rate This !!




Some times we need to open different views on SegmentControl i.e. on selecting different segment different views should get open. So to achieve that I will show you one of the easy way to do it.



So, now lets start and learn how we can do it in a few easy steps.


Step 1: Create a project with Single View name it what your desire. I took "SubAndSupeView" with ARC check on and unit test and story board uncheck.


Step 2:  Open your nib file and put a Segment Control on it. I am taking 3 segments in Segment Control namely "First View", "Second View", and "Third View". i.e. on clicking each Segment will open different Views.


Step 3:  Drag and Drop a View in nib file and add Outlet corresponding to it. I named it "containerView". This view will cover the whole nib file leaving Segment Control area. See fig 1. The red Color is nothing but the newly container view added in nib file. This will be the view where we add and remove other views hence is called Container View or in IOS we can say this will be the superView.



Screen shot 2012-06-26 at 6.54.30 AM.png


Step 4: Now drag and drop a Label on it and give Text "View 1", so that we can recognize this view.


Step 5: Now add 2 more views in nib file (as in fig 2) but not on mainView but separate on nib file and change their background color to see clear difference between transition of views and put label on it with text "View 2" and "View 3". And add outlet view2, view3 respectively.


Screen shot 2012-06-26 at 6.53.45 AM.png


Step 6: Now we completed whole designing part now only one thing is left and thats the action event for Segment Control. So create a method

-(IBAction)SegmentToggle:(UISegmentedControl*)sender; 


and connect it with the "valueChanged" event of the Segment Control.


Step 7: Now its coding Part i.e. implementing action method. So here is the body of the action Method.


-(IBAction)SegmentToggle:(UISegmentedControl*)sender

{

    if (sender.selectedSegmentIndex==0) {

        [view2 removeFromSuperview];

        [view3 removeFromSuperview];

    

    }

    else if (sender.selectedSegmentIndex==1) {

       

        [view3 removeFromSuperview];

        [containerView addSubview:view2];

    }

    else if (sender.selectedSegmentIndex==2) {

        [view2 removeFromSuperview];

        [containerView addSubview:view3];

    }


}



In above function we did nothing. Just we check selectedIndex of the Segment Control.


>> If SelectedIndex==0 then we need nothing to add as it already added in view but if we come from other segment then it wont be seen and thus we must remove other view added on it. Since we don't know which view is currently on containerView so I remove other 2 views.


removeFromSuperview  will unlinks the receiver from its superview and its window and removes it from the responder chain.


>> If SelectedIndex==1 then we need to remove only view3 and add view2 on container View.


>> If SelectedIndex==2 then we need to remove only view2 and add view3 on container View.


You can also make View1 separately rather than fixed on container View. I just do it to demonstrate you that we can do like this also. 


Step 8: Save all Project


Step 9: What are you waiting for just run the project. and see the output.




NOTE: If you need to open different ViewControllers on different Segment than in viewDidLoad its better to create an NSArray of UIViewController so that only one instance will be present of each Controller thus saving memory from running out and put this controllers in array cell.


Also take a variable currentController of UIViewController type in .h file which will keep the current selected View.


EXAMPLE :


 ViewController1 *view1=[[ViewController1 alloc]initWithNibName:@"ViewController1" bundle:nil];

      ViewController2 * view2 =[[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];

      ViewController3 * view3 =[[ViewController3 alloc]initWithNibName:@"ViewController3" bundle:nil];

   

    viewControllers=[NSArray arrayWithObjects: view1, view2, view3,nil]; // Add viewController in Array viewControllers which is declared in .h file as this is used across the scope of this function.

    currentController= view1;// make view 1 to be the current view. In this there is no view added on ContainerView by default like we did in above example. We add View1 directly on containerView

    [ContainerView addSubview: view1.view]; // Adding this view as subview on containerView. But remember View and hence view1.view.



And this is how the Action Event will look like:-


-(IBAction)segmentControlTabChange:(UISegmentedControl*)sender

{

    UIViewController *selectedView=nil;

    if (sender.selectedSegmentIndex==0) {

        selectedView= [viewControllers objectAtIndex:0]; // retrieve object at index 0 from viewControllers array

    }

    else if(sender.selectedSegmentIndex==1)

    {

        selectedView= [viewControllers objectAtIndex:1]; // retrieve object at index 1 from viewControllers array

    }

    else if(sender.selectedSegmentIndex==2)

    {

        selectedView= [viewControllers objectAtIndex:2]; // retrieve object at index 2 from viewControllers array

    }

    

    [currentController.view removeFromSuperview]; // remove Current displaying view from superView

    currentController=selectedView; // make selected View to be the current View

    [ContainerView addSubview:selectedView.view]; // Add newly selected view on container View

        

}


You can apply same concept ob button click, custom tab bar etc.


Hope this tutorial helps you for doing such type of work in future. Any feed back will be highly appreciated.


Posted Queries
Posted By: Soniya Vishwakarma Reply:Opening different Views on each Segment of S
01 Aug 2012
@anju singh:

Try and googling for customizing the UISegmentControl. You can easily customize the segment Control. I will try to put a tutorial on it soon.

Posted By: Soniya Vishwakarma Opening different Views on each Segment
09 Oct 2012
@ Roma Safin: Roma don't get confused the code will work on any version. The mistake you are committing is in prototype of method i.e. -(IBAction)togleInfo:(id)sender as the parameter is of "id" type which obviously not have "selectedSegmentIndex" property. Sender is nothing the reference of calling object. So, you can do 3 things:

  • Make IBOutlet of UISegmentControl and call "selectedSegmentIndex" property with this outlet. But we can do this without outlet so no need to create it. But if you want you can.

  • Type Caste "id" to UISegmentControl like 

-(IBAction)segmentControlTabChange:(id)sender

{

UISegmentedControl *segment=(UISegmentedControl*)sender;

    UIViewController *selectedView=nil;

    if (segment.selectedSegmentIndex==0) {

        selectedView= [viewControllers objectAtIndex:0]; // retrieve object at index 0 from viewControllers array


......

    }

  • Last, the style I adopted which you missed while coding is making parameter already according to calling objects. This will avoid you doing any extra work

-(IBAction)segmentControlTabChange:(UISegmentedControl*)sender

{

    UIViewController *selectedView=nil;

    if (sender.selectedSegmentIndex==0) {

        selectedView= [viewControllers objectAtIndex:0]; // retrieve object at index 0 from viewControllers array

    }


See my red Color text.

Try this. Hope this will help you.
Posted By: Soniya Vishwakarma WebView on TextView
12 Oct 2012
@ManojYadavAllahabad: Manoj I dont know why you add webview in textview. If you want to show html data then directly use webview and if you want to allow user to write and format his text then you can make rich editor using web view. You can surf on net for that. If you don't need scrolling then in textview and webView you can disable it by Uncheck the checkbox "Scrolling Enabled" in attribute Inspector or by code  
txtView.scrollEnabled=NO;

or in web view you can use CSS format your page, setting page height, width, padding etc.

NSString *varMySheet = @"var mySheet = document.styleSheets[0];";

NSString *addCSSRule =  @"function addCSSRule(selector, newRule) {"

"if (mySheet.addRule) {"

"mySheet.addRule(selector, newRule);" // For Internet Explorer

"} else {"

"ruleIndex = mySheet.cssRules.length;"

"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"   // For Firefox, Chrome, etc.

"}"

"}";

NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')",webView.frame.size.heightwebView.frame.size.width];

NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];

 

NSString *setTextSizeRule = [NSStringstringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",epubviewcontroller.currentTextSize];

[webView stringByEvaluatingJavaScriptFromString:varMySheet];

[webView stringByEvaluatingJavaScriptFromString:addCSSRule];

    

[webView stringByEvaluatingJavaScriptFromString:insertRule1];

[webView stringByEvaluatingJavaScriptFromString:insertRule2];

[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];

Posted By: Soniya Vishwakarma mail application
12 Oct 2012
@ManojYadavAllahabad: You can develop your own mail application by using smtp protocol to send mail to the respective server. Here is the code for it :

Posted By: Soniya Vishwakarma Using multiple Phone Numbers in Custom ALert View
12 Oct 2012
@ManojYadavAllahabad: Why don't you try UITableView in your Custom Alert don't show separator line in tableview. Make arrays of phone number and just give it to TableView with icon etc. so it make rows accordingly and it will be more manageable.
Posted By: anju singh Reply:Opening different Views on each Segment of S
01 Aug 2012
I want to implement custom colors in segmented control selected and unselected indexes. i have done it for the selected index but i havn't understands how to do for unselected indexes.
Posted By: Er. ManojYadav Allahabad Reply:Reply:Reply:Opening different Views on each
12 Oct 2012
How to create/implement/connect the all mail(gmail,yahoo,microsoft exchange,mobile me,Aol...) application in any application..and open in web browser...... if available predefinde file as json,or FBConnect type .. then post for mail application ...(ios 5.0),
Posted By: Er. ManojYadav Allahabad Reply:Reply:Opening different Views on each Segmen
12 Oct 2012
In my application based upon totally API. in my application have more shops and all shops have more phone number . but some shops have phone number1,phone number2 but not have phone no.3,4 and 5. and i access all data by web services and show all phone no. in custom alert view. but when we click another shops then come only phone no.1,3 but not have phone2,4,5. then created phone 2,4,5 button left space empty. but i want that empty space shifted by another existing phone no.. how to hide and remove this problem .and some shops have all phone number.
Posted By: Er. ManojYadav Allahabad Reply:Reply:Reply:Opening different Views on each
12 Oct 2012
i want to show annotation in gmap. but i want all annotation images are different . what is the code for this program...
Posted By: Er. ManojYadav Allahabad Reply:Opening different Views on each Segment
12 Oct 2012
How we can use WebView on TextView.. NSString *str=@"................................................................................................"; UIWebView *wv=[[UIWebView alloc]initWithFrame :CGRectMake(50,50,100,100)]; UITextView *tv=[[UITextView alloc]initWithFrame:CGRectMake(10,10,220,220)]; [self.view addSubview:tv]; [tv addSubview:wv]; [wv loadHTMLString:str baseURL:nil]; *** we do not want to scrolling left to right in webview. what will be code ..
12