在iphone中将字符串从一个视图复制到另一个视图

时间:2011-09-09 10:27:59

标签: iphone objective-c ios xcode

嗨,我是iphone开发新手。

我正在尝试使用示例,我需要从viewController的文本字段中复制字符串,并使用Label在下一个视图中显示它。

在第一个视图中,文本字段旁边有一个按钮 我无法解决显示BAD ACESS的任何问题,任何人都可以帮我解决这个问题。 让我知道我在做什么错误。

谢谢。

//copystring.h

@interface CopystringViewController : UIViewController{

    UITextField *myTextField;
    NSString *somestring;
}

@property(nonatomic,retain)IBOutlet UITextField *myTextfield;

@property(nonatomic,retain)NSString *somestring;

-(IBAction)next:(id)sender;

//.m file

@synthesize myTextfield,somestring;


-(IBAction)next:(id)sender;
{

    NextView * next = [[NextView alloc] initWithNewString: myTextField.text];

    [self.navigationController presentModalViewController: next animated: YES];

}

NextView.h


@interface NextView : UIViewController{

    UILabel  *string2;

}
@property(nonatomic,retain)IBOutlet UILabel *string2;

- (id)initWithNewString:(NSString*)someString;


//.m file

@synthesize string2;


 -(id)initWithNewString:(NSString*)someString {

    string2 = someString;
 return self;

}

7 个答案:

答案 0 :(得分:4)

只需替换method.it即可运行。

-(IBAction)next:(id)sender; {

    NextView * next = [[NextView alloc] initWithNibName:@"NextView" bundle:nil];
    next.string2=myTextField.text;
    [self.navigationController presentModalViewController:next animated: YES];

}

答案 1 :(得分:1)

hey string2是一个标签,所以请尝试string2.text = somestring;

 -(id)initWithNewString:(NSString*)someString {

string2.text = someString;

 return self;

 }

答案 2 :(得分:1)

看起来问题是您正在为UILabel(string2)分配NSString(someString)。

AppleVijay的答案应该可以解决问题。

如果您不喜欢ObjC中的点符号,您也可以这样写:

[string2 setText:someString]

答案 3 :(得分:1)

你可以向delagate h / m文件声明你的某些字符串,因为它将成为全局字符串。你可以和appdaligateobj.stringname一起使用它。

你不需要init你可以直接添加到viewdidload的下一个视图。

string2.text = AppDalegateobj.stringName。

答案 4 :(得分:1)

委托是像这样移动数据的常用方法。我写了一篇非常simple example project来表明这一点。

答案 5 :(得分:0)

您需要初始化查看并保留字符串:

-(id)initWithNewString:(NSString*)someString {
  self = [super init];

  if (self) {
    string2.text = someString;
  }
 return self;

}

答案 6 :(得分:0)

/

/AppDelegate.h

NSString *String1;

@property (nonatomic, retain) NSString * String1;

//AppDelegate.m
@synthesize String1

//ViewController1.h

AppDelegate * objAppDelegate;
UILable *lblstring;


@property (nonatomic, retain) UILable *lblstring;

//ViewController1.m
@synthesize lblstring

//On View Did Load.
objAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
lblstring.text  = objAppDelegate.String1;