Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-16-2010, 03:11 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 72
santosh_kumar is on a distinguished road
Default Switching the views in iphone

Hi all, I created a segmented control with 2 segments(button,textfield).I need to load only the selected segment on to the screen below the segmented control.When i select button first and textfield second, button should not appear on the screen and is same for the textfield. I tried with "hidden" and "removeFromSuperview" but not worked. So please help me.

CODE:
// segmentedControl.m which is a UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];

UISegmentedControl *segmentcontrol=[[ UISegmentedControl alloc ] initWithItems:
[NSArray arrayWithObjects:@"Buttons",@"TextField",@"Picker" ,@"TableView",nil]];

segmentcontrol.frame=CGRectMake(100,10,600,80);
segmentcontrol.selectedSegmentIndex=6;

[segmentcontrol addTarget:self
action:@selector(action
forControlEvents:UIControlEventAllEvents];

[self.view addSubview:segmentcontrol];
}


- (void)actionid)sender
{

NSLog(@"Segment %d selected", [sender selectedSegmentIndex]);

iButtons *button=[[iButtons alloc] initWithFrame:CGRectMake(0,200,400,400)];

iTextField *textfield=[[iTextField alloc]initWithFrame:CGRectMake(0,200,400,400)];


if ([sender selectedSegmentIndex]==0)
{

[button mymethod:NO];
[self.view addSubview:button];

}



if([sender selectedSegmentIndex]==1)
{
[button mymethod:YES];
[textfield mymethod]; // mymethod implements textfield
[self.view addSubview:textfield];

}

}

//iButtons.m file UIView File

-(id)initWithFrameCGRect)frame
{
if ((self = [super initWithFrame:frame ]))
{

NSLog(@"inside init.iButtons");
}
return self;
}


-(void)mymethodBOOL)hide
{

NSLog(@"myMethod");

if(hide==NO)
{

label1=[[UILabel alloc] initWithFrame:CGRectMake(100,100,300,60)];
label1.text=@"RoundedRectangle";
label1.textColor=[UIColor whiteColor];
label1.backgroundColor=[UIColor blackColor];
label1.font=[UIFont systemFontOfSize:30];


[self addSubview:label1];


roundedRectangle=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[roundedRectangle addTarget:self action:@selector(buttonclick) forControlEvents:UIControlEventTouchDown];
roundedRectangle.frame=CGRectMake(480,100,100,90);
[roundedRectangle setTitle:@"Button" forState:UIControlStateNormal];
[self addSubview:roundedRectangle];

}

if(hide==YES)
{


[label1 removeFromSuperview];
[roundedRectangle removeFromSuperview];

}



return self;

}





Thanks in advance.
Santosh.
santosh_kumar is offline   Reply With Quote
Old 08-17-2010, 07:10 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 72
santosh_kumar is on a distinguished road
Default I got the reason

In the previous posted program, i removed an instance( [label1 removeFromSuperview]...) iButton but not the iButton. So, [iButton removeFromSuperView] is the right solution.


- (void)actionid)sender
{

id existingview1 = [self.view viewWithTag:100];
id existingview2 = [self.view viewWithTag:200];

if(existingview1 != nil)
[existingview1 removeFromSuperview];

else
[existingview2 removeFromSuperview];



if([sender selectedSegmentIndex]==0)
{
buttons = [[iButtons alloc] initWithFrame:CGRectMake(0,200,400,400)];
buttons.tag = 100;
[self.view addSubview:buttons];

[buttons mymethod];
}
else
{

//Text
textFields = [[iTextField alloc] initWithFrame:CGRectMake(0,200,400,400)];
textFields.tag = 200;
[self.view addSubview:textFields];
[textFields mymethod];

}
}
santosh_kumar is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone & ipod touch apps

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 354
4 members and 350 guests
givensur, linkmx, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:48 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0