开发者

Problem with views and button iPhone application

I have this scenario, I have 4 views. v1,v2,v3,v4.

I am in view v1, which is table view when i press any cell it takes me to another view v2. Fine.

Then in v2 i have some textFields and button B2. When i press button B2(i have set in its method to save textField data and pushViewController function) it takes me to v3.

I have a button in v3 as well B3. This button saves data on v3 and takes me to view 4 i.e. v4.

In v4 all textfields of v2 and v3 is shown. I have no problem. B开发者_运维技巧ut when i press navigation back barbutton it takes me to v3 from v4. but now when i change textField value and press button on v3 again it does nothing.same as if press navigation bar button again i.e. back button it takes me v2 and button B2 does nothing.

Can anyone help me regarding this.

-(IBAction)buttonPress:(id)sender{ 
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; 
[defaults setObject:field1.text forKey:@"field1Key"]; 
[defaults setObject:field2.text forKey:@"field2Key" ]; 
[defaults synchronize]; 
if (co==nil) { 
    co=[[class4 alloc]initWithNibName:nil bundle:nil]; 
co.name=field1.text; co.phone=field2.text; 
[self.navigationController pushViewController:co animated:YES ]; 
[co release];
} –

}    


You are pushing ViewControllers inside the if (co ==nil) block. During the first navigation, the view controller is actually nil and so it worked. If you navigate back and press the button again, it will not work - it makes sense !

You should push view Controllers whether is it nil or not. You can do like this.

if (co==nil) { 
co=[[class4 alloc]initWithNibName:nil bundle:nil]; 

}

co.name=field1.text; co.phone=field2.text; 
[self.navigationController pushViewController:co animated:YES ]; 
[co release];

or make co=nil before checking for it.

co = nil;
if (co==nil) { 
co=[[class4 alloc]initWithNibName:nil bundle:nil]; 
co.name=field1.text; co.phone=field2.text; 
[self.navigationController pushViewController:co animated:YES ]; 
[co release];
} –


//do like this to all viewcontrollers action

-(IBAction)buttonPress:(id)sender{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

    [defaults setObject:field1.text forKey:@"field1Key"]; 

    [defaults setObject:field2.text forKey:@"field2Key" ];

    [defaults synchronize];

    if (co==nil) { 

        co=[[class4 alloc]initWithNibName:@"class4" bundle:nil]; 
        co.name=field1.text; 

        co.phone=field2.text; 

        [self.navigationController pushViewController:co animated:YES ];//updated
        [co release];           
    } 


    else {

        [self.navigationController pushViewController:co animated:YES ];

        [co release];
    }

}

//note this line co=[[class4 alloc]initWithNibName:@"class4" bundle:nil];


I found the answer to my question.

-(IBAction)buttonPress:(id)sender{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; [defaults  
setObject:field1.text forKey:@"field1Key"]; [defaults setObject:field2.text 
forKey:@"field2Key" ]; [defaults synchronize]; if (co==nil) { co=[[class4 
alloc]initWithNibName:nil bundle:nil]; co.name=field1.text; co.phone=field2.text; 
[self.navigationController pushViewController:co animated:YES ]; 
[self.navigationController pushViewController:co animated:YES ];[co release]; } co=nil;
//This point to be noted. } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜