0

(iPhone Development Xcode 4.3)

Ive got a textfield on a view controller and an NSString on another controller. What I'm trying to do is to allow the user to enter a URL in the textfield and pass that as the path in my other controller.

This is the NSString I'm currently using:

NSString * path = @"http://www.test.com";

So im looking to do something like:

NSString * path = INPUT FROM USER;

Hope someone can point me in the right direction.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Doop
  • 3
  • 1
  • 2

3 Answers3

1

use

 NSString *path = textField.text;

for that create IBOutlate for that textField

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
0
NSString *urlString = [NSURL URLWithString:textfield.text];
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
  • Im slightly confused. So basically if I set an IBOUTLET for the textfield on my TextBoxViewController, like so: @property (weak, nonatomic) IBOutlet UITextField *urltext. How do I define that textfields input on another controller ? – Doop Apr 09 '12 at 14:04
  • Use .h method pass ur string to another page. In next page like @property(weak,nonatomic) IBOutlet UITextField *urltext1; .in .m method like @synthesize urltext1; -(void)viewDidLoad{ NSLog(@"%@",yourText); for example read: http://stackoverflow.com/a/9867328/932011 – Ravi Kumar Karunanithi Apr 09 '12 at 15:02
0

Basically you will need two things:

  1. Get the string from user

As you've already known, all you need to do is to set your IBOutlet on your textfield and read the string.

  1. Pass it to another view controller

You have two options to do it:

First, you can add a string to your second view controller and set it property. Then create a custom -init to receive the url string. In your first view controller, when you create the second view controller(I assume that's the case), use your custom -init to set the value.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andURL:(NSString *) url
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization

    self.urlString = url;

    }
    return self;
}

You have another option: create a method on the second view controller.

so in your second view controller, do this:

-(void) setURL: (NSString *) url
{
    self.rulString = url;
}

and in your first view controller, do:

SecondViewController *secondVC = [SecondViewController alloc] init];

// get url string from user input and store it in a local string variable named tempURL
[secondVC setURL:tempURL];

Hope this helps.

Raymond Wang
  • 1,484
  • 2
  • 18
  • 33