我正在尝试在SearchCategoryChooserViewcontroller
中设置两个按钮,根据函数返回的值显示标题。但是,即使成功返回topCategoriesArray
,按钮似乎也会显示标题为“null”。这是我的设置:
SearchCategoryChooserViewController.m:
#import "SearchCategoryChooserViewController.h"
#import "SearchViewController.h"
@interface SearchCategoryChooserViewController ()
@end
@implementation SearchCategoryChooserViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *category1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
category1.frame = CGRectMake(10, 120, 300, 35);
[category1 setTitle: [NSString stringWithFormat:@"%@", self.topCategory1] forState:UIControlStateNormal];
[category1 addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: category1];
UIButton *category2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
category2.frame = CGRectMake(10, 180, 300, 35);
[category2 setTitle: [NSString stringWithFormat:@"%@", self.topCategory2] forState:UIControlStateNormal];
[category2 addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: category2];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
SearchCategoryChooserViewController.h:
#import <UIKit/UIKit.h>
#import "SearchViewController.h"
@interface SearchCategoryChooserViewController : SearchViewController
@end
SearchViewController.h:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import <Parse/PFCloud.h>
#import "CriteriaViewController.h"
@interface SearchViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *nextButtonOutlet;
@property (weak, nonatomic) NSString *topCategory1;
@property (weak, nonatomic) NSString *topCategory2;
@end
SearchViewController.m:
#import "SearchViewController.h"
@interface SearchViewController ()
@property (weak, nonatomic) IBOutlet UITextField *itemSearch;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@end
@implementation SearchViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.nextButtonOutlet addTarget:self action:@selector(nextButton:) forControlEvents:UIControlEventTouchUpInside];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)nextButton:(id)sender
{
if (self.itemSearch.text.length > 0) {
[PFCloud callFunctionInBackground:@"eBayCategorySearch"
withParameters:@{@"item": self.itemSearch.text}
block:^(NSDictionary *result, NSError *error) {
NSLog(@"'%@'", result);
NSArray *resultArray = [result objectForKey:@"results"];
NSDictionary *dictionary0 = [resultArray objectAtIndex:0];
NSNumber *numberOfTopCategories = [dictionary0 objectForKey:@"Number of top categories"];
NSDictionary *dictionary1 = [resultArray objectAtIndex:1];
NSNumber *topCategories = [dictionary1 objectForKey:@"Top categories"];
NSDictionary *dictionary2 = [resultArray objectAtIndex:2];
NSNumber *numberOfMatches = [dictionary2 objectForKey:@"Number of matches"];
NSDictionary *dictionary3 = [resultArray objectAtIndex:3];
NSNumber *userCategoriesThatMatchSearch = [dictionary3 objectForKey:@"User categories that match search"];
NSArray *topCategoriesArray = [dictionary1 objectForKey:@"Top categories"];
NSString *topCategory1 = [topCategoriesArray objectAtIndex:0];
NSString *topCategory2 = [topCategoriesArray objectAtIndex:1];
if (!error) {
// if 1 match found clear categoryResults and top2 array
if ([numberOfMatches intValue] == 1 ){
[self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self];
}
// if 2 matches found
else if ([numberOfMatches intValue] == 2){
[self performSegueWithIdentifier:@"ShowUserCategoryChooserSegue" sender:self];
//default to selected categories criteria -> send to matchcenter -> clear categoryResults and top2 array
}
// if no matches found, and 1 top category is returned
else if ([numberOfMatches intValue] == 0 && [numberOfTopCategories intValue] == 1) {
[self performSegueWithIdentifier:@"ShowCriteriaSegue" sender:self];
}
// if no matches are found, and 2 top categories are returned
else if ([numberOfMatches intValue] == 0 && [numberOfTopCategories intValue] == 2) {
[self performSegueWithIdentifier:@"ShowSearchCategoryChooserSegue" sender:self];
}
}
}];
}
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// if([segue.identifier isEqualToString:@"ShowSearchCategoryChooserSegue"]){
// SearchCategoryChooserViewController *controller = (SearchCategoryChooserViewController *) segue.destinationViewController;
// controller.itemSearch.text = self.itemSearch.text;
// }
}
@end
答案 0 :(得分:3)
有很多问题:
您应该更改代码:
@property (weak, nonatomic) NSString *topCategory1;
@property (weak, nonatomic) NSString *topCategory2;
到
@property (strong, nonatomic, copy) NSString *topCategory1;
@property (strong, nonatomic, copy) NSString *topCategory2;
或
//because strong is default
@property (nonatomic, copy) NSString *topCategory1;
@property (nonatomic, copy) NSString *topCategory2;
如果在使用NSMutableString值的情况下为NSString属性添加'copy'属性,则会更好。
因为使用ARC,如果对象没有至少一个强引用,它将被解除分配。如果您的对象只有weak
对另一个对象的引用,则它不是拥有它,即使您在对象中使用它也会被释放。但是strong
引用你的Object 拥有另一个对象,在你的对象生存之前它不会被释放。
您应该在执行segue之前设置属性值并更改此代码
NSString *topCategory1 = [topCategoriesArray objectAtIndex:0];
NSString *topCategory2 = [topCategoriesArray objectAtIndex:1];
到
self.topCategory1 = [topCategoriesArray objectAtIndex:0];
self.topCategory2 = [topCategoriesArray objectAtIndex:1];
并使用prepareForSegue中的值:
if([segue.identifier isEqualToString:@"ShowSearchCategoryChooserSegue"]){
SearchCategoryChooserViewController *controller = (SearchCategoryChooserViewController *) segue.destinationViewController;
controller.itemSearch.text = self.itemSearch.text;
controller.topCategory1 = self.topCategory1;
controller.topCategory2 = self.topCategory1;
}
答案 1 :(得分:0)
在我看来(从你已经粘贴到你的问题的代码中),你似乎永远不会设置你的&#34; topCategory1
&#34; SearchCategoryChooserViewController对象中的字符串属性。
这可以解释为什么你会看到&#34; null
&#34;在标签上。