开发者

how to copy a string from one view and display it in the next view?(iphone)

开发者 https://www.devze.com 2023-04-03 13:09 出处:网络
hi everyone i am new to iphone development. Actually i am trying with some sample app where i h开发者_运维百科ave a textField in the Viewcontroller and a Button,when i enter a string in the textfield

hi everyone i am new to iphone development.

Actually i am trying with some sample app where i h开发者_运维百科ave a textField in the Viewcontroller and a Button,when i enter a string in the textfield and press the button it should display the same string in NextView.so can anyone help me out in doing this.

i worked with transition between one view to another view,but i need copy string from Viewcontroller1 to NextView

@ViewController1

-(IBAction)next
{
    NextView *Nview = [[NextView alloc]initWithNibName:@"NextView" bundle:nil];

    [self.view addSubview:Nview.view];    
}


Set an ivar in your NextView called "newString" for example, then pass a string to that ivar form your first controller.

For Example, not tested (and this is one of many ways you can do this):

FirstView

NextView * next = [[NextView alloc] initWithNewString: myTextField.text];
[self.navigationController presentModalViewController: next animated: YES];

NextView

@synthesize newString

-(id)initWithNewString:(NSString*)someString
{
    newString = someString;
    return self;
}

Then throughout your NextView, just call upon newString wherever you want to get the value of the previous views textField.


-(IBAction)next
{
    NextView *Nview = [[NextView alloc]initWithNibName:@"NextView" bundle:nil];

    Nview.string_Object=string_to_copy;//declare string_Object in NextView.h

    [self.view addSubview:Nview.view];    
}


Try this: In your first view while naviagting:

    NextView *nav = [[NextView alloc] init];
    nav.textFieldValue = textField.text;        
    [self.navigationController pushViewController:nav animated:YES];

And in your NextView's .h file create a property:

@property(nonatomic, retain) NSString *textFieldValue;

And in .m file synthesize it like:

@synthesize textFieldValue;

Now you can use textFieldValue in NextView class

P.S: Don't forget to release it :)


As you are a beginner, this will be a good guidance for you to understand the exact flow and it will make you learn how to pass values from one class to another.


Here is an approach (haven't tested, but should work)

Give your textField a tag value like

textField.tag = 1;

and in NextView access it like

UITextField *parentViewTextField = (UITextField*)[self.superview viewWithTag:1]
0

精彩评论

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