iOS: NSString when uploaded to parse.com shows blank

泄露秘密 提交于 2019-12-14 04:23:11

问题


I have taken an NSString from one class to another to be saved on parse.com

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.

quiz.h

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


quiz.m
- (void)viewDidLoad {
    [super viewDidLoad];

    UITextView *textView = [[UITextView alloc] init];
    NSString *text = self.textField.text;
    selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
    sfvc.string = text;
}

- (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.h

@property (nonatomic, strong)  NSString *string;

selectfriendsviewcontroller.m

- (void)viewDidLoad {
[super viewDidLoad];

quizViewController *qvc = [[quizViewController alloc] init];
qvc.text = string;
UITextView *textfield = [[UITextView alloc] init];
string = textfield.text;
 NSLog(@"%@", self.string);
}

if i remove everything except the NSLog in the selectfriends.m file in viewdidload method, the file wont upload to parse. and i get the alertView showing and error saying not file or directory exists.

then i upload to parse.com

- (void) uploadMessage;
{
NSString *text =string;
NSString *fileType= @"text";
NSString * fileName= @"text.txt";

NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];

PFFile *file = [PFFile fileWithName:fileName data:data];

[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (error) {
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please try sending your message again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

       [alertView show];

1) the string does not pass as null as null does not show in the console at runtime. (nothing shows in the console even with the NSLog statement)

2) the file uploads to parse.com as a text.txt file but when i download the file to check the text the file is blank.

So in quizVC type a question then press next to go to selectfirendsVC then select friends and upload to parse.com

how can i upload the textfile to show the user inputted text in the UITextView in the quizVC?


回答1:


You don't want to communicate with just any instance of your SelectFriends vc, like the one created in your viewDidLoad. You must communicate with the instance that is going to be presented.

Remove the code in Quiz vc's viewDidLoad. Create a segue from Quiz vc to SelectFriends vc. In Quiz vc, implement prepareForSegue. In that method, interrogate the textView's text and set the string property on the segue.destinationViewController. That's the one that will be presented.

(A more general treatment of vc-vc communication here).



来源:https://stackoverflow.com/questions/31608689/ios-nsstring-when-uploaded-to-parse-com-shows-blank

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!