Ios 按钮值显示为";空";

Ios 按钮值显示为";空";,ios,objective-c,Ios,Objective C,我试图在我的SearchCategoryChooserViewcontroller中使用两个按钮,根据函数返回的值显示标题。然而,尽管成功地返回了topCategoriesArray,按钮似乎显示标题为“null”。以下是我的设置: SearchCategoryChooserViewController.m: #import "SearchCategoryChooserViewController.h" #import "SearchViewController.h" @interface S

我试图在我的
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
#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
SearchCategoryChooserViewController.h:

#import <UIKit/UIKit.h>
#import "SearchViewController.h"

@interface SearchCategoryChooserViewController : SearchViewController

@end
#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
在我看来(从您粘贴到问题中的代码来看),您从未在SearchCategoryChooserViewController对象中设置“
topCategory1
”字符串属性


这可以解释为什么您在标签中看到“
null
”。

存在多个问题:

您应该更改代码:

@property (weak, nonatomic) NSString *topCategory1;
@property (weak, nonatomic) NSString *topCategory2;

在使用NSMutableString值的情况下,最好为NSString属性添加“copy”属性

因为使用ARC时,如果对象没有至少一个强引用,它将被解除分配。如果您的对象对另一个对象只有一个
引用,则该对象不是您的所有者,即使您在对象中使用它,它也将被解除分配。但是使用
strong
reference,您的对象拥有另一个对象,并且在您的对象存在之前,它不会被解除分配

在使用更改此代码执行序列之前,您应该设置属性值

NSString *topCategory1 = [topCategoriesArray objectAtIndex:0];
NSString *topCategory2 = [topCategoriesArray objectAtIndex:1];

并使用PrepareForsgue中的值:

if([segue.identifier isEqualToString:@"ShowSearchCategoryChooserSegue"]){
    SearchCategoryChooserViewController *controller = (SearchCategoryChooserViewController *) segue.destinationViewController;
    controller.itemSearch.text = self.itemSearch.text;
    controller.topCategory1 = self.topCategory1;
    controller.topCategory2 = self.topCategory1;
}

您应该将它们声明为
copy
,以防将可变字符串指定给属性。有关更多信息,请参阅。不要将它们声明为非原子的,因为这使用了默认的
assign
.Edited:)谢谢@rich这一切都是有意义的,但是
prepareForSegue
中的if语句给了我一个错误
使用未声明的标识符SearchCategoryChooserViewController
。我应该在哪里声明该标识符?查看苹果的。您必须单击segue并设置标识符属性,就像NextScreen IDMy segue已经设置了正确的标识符一样,这里的错误似乎是指这部分:
SearchCategoryChooserViewController*控制器=(SearchCategoryChooserViewController*)segue.destinationViewController
NSString *topCategory1 = [topCategoriesArray objectAtIndex:0];
NSString *topCategory2 = [topCategoriesArray objectAtIndex:1];
self.topCategory1 = [topCategoriesArray objectAtIndex:0];
self.topCategory2 = [topCategoriesArray objectAtIndex:1];
if([segue.identifier isEqualToString:@"ShowSearchCategoryChooserSegue"]){
    SearchCategoryChooserViewController *controller = (SearchCategoryChooserViewController *) segue.destinationViewController;
    controller.itemSearch.text = self.itemSearch.text;
    controller.topCategory1 = self.topCategory1;
    controller.topCategory2 = self.topCategory1;
}