我使用准备segue将字符串发送到下一个控制器。它被设置为一个字符串直到被发送(根据NSLog),但是一旦我将它记录在下一个控制器上它就是一个NSObject而不是一个字符串,因此该值变为null。
这是第一个视图控制器,bodyText字段是我试图发送到下一个视图控制器的:
#import "GFAddPostTextViewController.h"
#import "GFSelectGroupViewController.h"
@interface GFAddPostTextViewController ()
@property (weak, nonatomic) IBOutlet UITextField *bodyTextField;
@end
@implementation GFAddPostTextViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"selectGroup"]) {
GFSelectGroupViewController *destViewController = segue.destinationViewController;
destViewController.bodyText = self.bodyTextField.text;
NSLog(@"%@", destViewController.bodyText);
}
}
@end
以下是接收视图控制器的头文件:
#import <UIKit/UIKit.h>
@interface GFSelectGroupViewController : UITableViewController
@property (weak, nonatomic) IBOutlet NSString *bodyText;
@end
最后接收视图控制器的实现文件(保存方法是我调用self.bodyText的地方,因为它认为bodytext为null,所以它出错了):
#import "GFSelectGroupViewController.h"
#import <AFNetworking.h>
#import "GFCredentialStore.h"
#import "GFGroupResponseSerializer.h"
#define kBaseURL "http://someserver.com/"
#define kGroupURL "groups.json"
#define kPostURL "posts.json"
@interface GFSelectGroupViewController ()
@property (nonatomic, strong) NSArray *groups;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@end
@implementation GFSelectGroupViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
GFCredentialStore *credentialStore = [[GFCredentialStore alloc] init];
NSString *authToken = [credentialStore authToken];
NSLog(@"%@", authToken);
__weak typeof(self)weakSelf = self;
[self.saveButton setTarget:self];
[self.saveButton setAction:@selector(save:)];
NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kGroupURL];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [GFGroupResponseSerializer serializer];
[manager.requestSerializer setValue:authToken forHTTPHeaderField:@"auth_token"];
NSLog(@"%@", manager.requestSerializer.HTTPRequestHeaders);
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
__strong typeof(weakSelf)strongSelf = weakSelf;
strongSelf.groups = (NSArray *)responseObject;
[strongSelf.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
-(void)save:(id)sender {
NSString * postURL = [NSString stringWithFormat:@"%s%s", kBaseURL, kPostURL];
NSString *body = self.bodyText;
NSLog(@"%@", body);
NSDictionary *params = @{ @"post[body]" : body};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:postURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
[self dismissViewControllerAnimated:NO completion:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
iOS新手,所以任何帮助都非常感谢。感谢。
答案 0 :(得分:1)
问题出在财产申报中。您将字符串声明为weak
(和IBOutlet
,它也不应该这样。)
在ARC规则下,只要所有强指针不再指向它,就会从内存中删除此对象。因为您在prepareForSegue
方法中设置了它,所以在访问它之前它会被清零。因此,请将您的财产声明更改为:
@interface GFSelectGroupViewController : UITableViewController
@property (strong, nonatomic) NSString *bodyText;
@end
在prepareForSegue
方法中,设置如下:
...
GFSelectGroupViewController *destViewController = segue.destinationViewController;
destViewController.bodyText = [self.bodyTextField.text copy];
...
还可以帮助自己进行调试,在viewDidLoad
GFSelectGroupViewController
方法中打印变量,以便正确复制它。