如何在nextViewController字符串中存储textField值

时间:2014-09-11 05:43:09

标签: ios objective-c iphone ios7 nsstring

我有文本字段,我想将此值存储在nextViewController中的字符串中,并使用url发送字符串。怎么可能?

if ([textField.text length]== 0) {
    [[[UIAlertView alloc] initWithTitle:@"DigiPhoto" message:@"Please Enter The Code" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
} else {

    DigiPhotoTableViewController * photoView = [[DigiPhotoTableViewController alloc]init];
    [self.navigationController pushViewController:photoView animated:YES];

}

在NextViewController中我想做这个如何添加textField请帮助我。

scanCode = textField.text;
NSString * urlString=[NSString stringWithFormat:@"url %@",scanCode];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (!conn)
{
    responseData = nil;
    NSLog(@"Connection Error");
}

2 个答案:

答案 0 :(得分:1)

在主视图控制器

中创建一个属性
@interface DigiPhotoTableViewController : UIViewController
@property (nonatomic, strong) NSString* dataString;
@end

并使用mainViewcontroller对象从其他视图控制器发送值,如第一个导入DigiPhotoTableViewController

使用主类对象后发送值

if ([textField.text length]== 0) {
    [[[UIAlertView alloc] initWithTitle:@"DigiPhoto" message:@"Please Enter The Code" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
} else {

    DigiPhotoTableViewController * photoView = [[DigiPhotoTableViewController alloc]init];
    photoView.dataString = textField.text;;
    [self.navigationController pushViewController:photoView animated:YES];

}

有关详细信息,请访问此链接Pass a value from one ViewController to another ViewController

答案 1 :(得分:0)

创建一个名为scanCode的属性,如下所示

if ([textField.text length]== 0) {
    [[[UIAlertView alloc] initWithTitle:@"DigiPhoto" message:@"Please Enter The Code" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
} else {

    DigiPhotoTableViewController * photoView = [[DigiPhotoTableViewController alloc]init];
    photoView.scanCode=textField.text;//assigning textfield value to scanCodeString
    [self.navigationController pushViewController:photoView animated:YES];

}

//Next View COntroller..Modify code as below.

@property(nonatomic,strong) NSString *scanCode;

//textField.text=scanCode;
NSString * urlString=[NSString stringWithFormat:@"url %@",scanCode];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (!conn)
{
    responseData = nil;
    NSLog(@"Connection Error");
}

希望它可以帮助你..