-2

I have taken an NSString from one class to another

I have a quiz view controller where a user enters a question in a UITextView and then they press next to go to the select friends view controller where they select a user and then send the question over parse.com

quiz.h

@property (nonatomic,strong) IBOutlet UITextView *textField;
@property (nonatomic, strong)  NSString *text;



quiz.m
- (IBAction)next:(id)sender {

NSString *text = self.textField.text;

if ([text length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                            message:@"Enter some text"
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    } else {
        selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
        sfvc.string = text;
    }

}



selectfriendsviewcontroller.h
@property (nonatomic, retain)  NSString *string;


selectfriendsviewcontroller.m

@synthezise string;
- (void)viewDidLoad {
[super viewDidLoad];

quizViewController *qvc = [[quizViewController alloc] init];
qvc.text = string;
UITextView *textfield = [[UITextView alloc] init];
string = textfield.text;
}

why does the string show null?? in the quiz.m when i press next the string passes as null, any ideas as to how i can fix?

sTg
  • 4,313
  • 16
  • 68
  • 115
Harry
  • 17
  • 6
  • you initialized the UITextView in viewDidLoad. At that time there's no text in textView. Then how could you pass textfield.text to string ? – Sushil Sharma Jul 23 '15 at 09:35
  • Also you alloc init quizViewController in viewDidLoad. and qvc.text is null at that time. – Sushil Sharma Jul 23 '15 at 09:38
  • thanks for the reply, im very new to iOS. how can i solve the issue should i alloc and init a uitextview in the quiz.m? under the IBAction? – Harry Jul 23 '15 at 09:46
  • You are passing string from quizViewController to selectfriendsviewcontroller right ? – Sushil Sharma Jul 23 '15 at 09:48
  • at the moment i am still testing and something still does not seem to working. the nsstring does not display as null but the textfile i upload to parse is blank. – Harry Jul 24 '15 at 09:22
  • You should post a question for this. Then we will be able to help you. – Sushil Sharma Jul 24 '15 at 10:27
  • @sushil i have posted the question http://stackoverflow.com/questions/31608689/ios-nsstring-when-uploaded-to-parse-com-shows-blank – Harry Jul 24 '15 at 10:54

2 Answers2

0

In quizViewController under next button action

- (IBAction)next:(id)sender {

    if ([text length] == 0) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                                message:@"Enter some text"
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        } else {
            selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
            sfvc.string = self.textField.text;
        }
      }

Then in selectFriendsViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"string %@",self.string);
}

Also check if the outlet of textView is connected to UITextView or not. If not then connect it. And selectFriendsViewController.h , create property @property (nonatomic, strong) NSString *string; Hope it helps you.

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
0

selectfriendsviewcontroller.m you can access the value of the string from the string property (by the way avoid generic names like string ) also if you use Xcode > 4.4 you can skip @synthezise

in selectfriendsviewcontroller.m

-(void)viewDidLoad 
{
  [super viewDidLoad];
  NSLog(@"%@",self.string);
}
Alaeddine
  • 6,104
  • 3
  • 28
  • 45