2013-02-19 3 views
0

Я пытаюсь изменить UITextView.text из SecondViewController в ViewController. Я могу это сделать, если это был тип NSString в SecondViewController, выполнив:Как изменить UITextField текст View2 из View1?

SVC.string = @"test"; 

Проблема заключается в том, что:

  • messageBox.text не меняет
  • SVC.messageBox.text возвращается (нуль)

Одним из методов в ViewController.m:

SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 
    SVC.messageBox.text = @"changed"; 
    NSLog(@"SVC: %@", SVC.messageBox.text); // result = (null) 

    [self presentViewController:SVC animated:YES completion:NULL]; 

SecondViewController.h:

#import <UIKit/UIKit.h> 

@interface SecondViewController : UIViewController 

@property (nonatomic) IBOutlet UITextView *messageBox; 


@end 

SecondViewController.m:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    self.messageBox.text = @"first"; 
} 

Как получить более с этим?

ответ

0

Проблема с вашим кодом - это доступ к uicontrols, которые еще не созданы в памяти. Вам необходимо изменить порядок выполнения ваших заявлений, как это ...

SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 

    [self presentViewController:SVC animated:YES completion:NULL]; 


    SVC.messageBox.text = @"changed"; 
    NSLog(@"SVC: %@", SVC.messageBox.text); 
0

Установите @property (nonatomic) IBOutlet UITextView *messageBox; на номер @property (nonatomic, strong) IBOutlet UITextView *messageBox; и проверьте, подключили ли вы его с помощью UItextView. И не переустанавливайте его в viewDidLoad. Еще одна вещь, которую вы можете попробовать:

SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 
[self presentViewController:SVC animated:YES completion:NULL]; 
SVC.messageBox.text = @"changed"; 
NSLog(@"SVC: %@", SVC.messageBox.text); // result = (null) 

Наряду с тем, что упомянуто выше.

2

Попробуйте это, Один из методов в ViewController.m:

SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 
SVC.strMessage= @"changed"; 
[self presentViewController:SVC animated:YES completion:NULL]; 

SecondViewController.h:

#import <UIKit/UIKit.h> 

@interface SecondViewController : UIViewController 
{ 
    IBOutlet UITextView *messageBox; 
} 
@property (nonatomic) NSString *strMessage; 
@end 

SecondViewController.m: @synthesize strMessage;

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 

self.messageBox.text = strMessage; 
} 
1

Вы также можете использовать делегатов для этого.

Пример:

Viewcontroller2nd.h

@protocol SecondViewControllerDelegate <NSObject> 
@optional 
-(void) changeLabel:(NSString*)str; 
@end 

@interface ViewController2nd : UIViewController{ 

    IBOutlet UIButton *bttn; 
    id <SecondViewControllerDelegate> delegate; 

} 

@property (retain) id delegate; 

@end 

Viewcontrollerone.h

@interface ViewController : UIViewController <SecondViewControllerDelegate> 
{ 
    IBOutlet UILabel *lbl; 

} 
-(IBAction)passdata:(id)sender; 

@end 

Viewcontrollerone.m

-(void) changeLabel:(NSString*)str{ 
    lbl.text = str; 
} 

Подробнее о делегатов: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Надеется, что это помогает ...

0

только свойство-синтезирует строка ANDF просто присвоить эту строку yourTextView.text. Например, см. Ниже.

@interface SecondViewController : UIViewController{ 
     NSString *strMsgText; 
} 
@property(nonatomic, retain) NSString *strMsgText; 
@end 

@implementation SecondViewController 
@synthesize strMsgText; 

после этого, когда вы настоящий SecondViewController в то время присвоить значение, как сильфон ..

SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; 
    SVC.strMsgText = @"changed"; 
    [SVC.strMsgText retain]; 


    [self presentViewController:SVC animated:YES completion:NULL]; 

и viewDidLoad: метода SecondViewController просто назначить эту строку messageBox(TextView), как сильфон ...

- (void)viewDidLoad 
{ 
    messageBox.text = strMsgText; 
} 
Смежные вопросы