【发布时间】:2014-10-17 05:52:44
【问题描述】:
我想从我的SearchViewController 发送一个布尔值didAddNewItem 到MatchCenterViewController,然后根据布尔值的状态运行一个函数。我尝试将YES 的didAddNewItem 值发送到我的目的地MatchCenterViewController,但它似乎没有正确发送,因为下面的函数永远不会运行。
这是我从 SearchViewController 发送它的方式(经过编辑以反映 Rob 的回答):
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ShowMatchCenterSegue"]) {
_didAddNewItem = YES;
MatchCenterViewController *controller = (MatchCenterViewController *) segue.destinationViewController;
NSLog(@"we're about to set controller values before segueing to MC");
// Send over the matching item criteria
controller.itemSearch = self.itemSearch.text;
controller.matchingCategoryId = self.matchingCategoryId1;
controller.matchingCategoryMinPrice = self.matchingCategoryMinPrice1;
controller.matchingCategoryMaxPrice = self.matchingCategoryMaxPrice1;
controller.matchingCategoryCondition = self.matchingCategoryCondition1;
controller.matchingCategoryLocation = self.matchingCategoryLocation1;
controller.itemPriority = self.itemPriority;
[self.tabBarController setSelectedIndex:1];
}
}
这是我尝试在目的地 MatchViewController 中使用它的地方:
- (void)viewDidAppear:(BOOL)animated
{
if (_didAddNewItem == YES) {
NSLog(@"well then lets refresh the MC");
// Start loading indicator
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
[self.view addSubview: activityIndicator];
[activityIndicator startAnimating];
// Disable ability to scroll until table is MatchCenter table is done loading
self.matchCenter.scrollEnabled = NO;
_matchCenterDone = NO;
// Add new item to MatchCenter Array with the criteria from the matching userCategory instance, plus the search term
[PFCloud callFunctionInBackground:@"addToMatchCenter"
withParameters:@{
@"searchTerm": self.itemSearch,
@"categoryId": self.matchingCategoryId,
@"minPrice": self.matchingCategoryMinPrice,
@"maxPrice": self.matchingCategoryMaxPrice,
@"itemCondition": self.matchingCategoryCondition,
@"itemLocation": self.matchingCategoryLocation,
@"itemPriority": self.itemPriority,
}
block:^(NSString *result, NSError *error) {
if (!error) {
NSLog(@"'%@'", result);
self.matchCenterArray = [[NSArray alloc] init];
[PFCloud callFunctionInBackground:@"MatchCenter3"
withParameters:@{}
block:^(NSArray *result, NSError *error) {
if (!error) {
_matchCenterArray = result;
[_matchCenter reloadData];
[activityIndicator stopAnimating];
// Reenable scrolling/reset didAddNewItem bool
_matchCenterDone = YES;
self.matchCenter.scrollEnabled = YES;
//_didAddNewItem = NO;
NSLog(@"Result: '%@'", result);
}
}];
}
}];
}
}
我确保它已正确设置为两个 ViewController 的标头中的属性,所以我不确定为什么它没有正确设置目标 VC 中的值。我知道addToMatchCenter 函数可以正常运行而没有错误,所以它应该可以工作。
@property (assign) BOOL didAddNewItem;
【问题讨论】:
-
什么时候推送 MatchCenterViewController?如果您在执行块之前推送此视图控制器,则不会为 didAddNewItem 分配值。
-
这里的问题在于块,因为当执行 segue 时,无论调用什么块,都会调用 MatchCenterViewController 的 viewDidload 方法。所以你的 bool 值不会被设置,并且你的 viewDidlaod 方法被调用,它没有你的 bool 值。因此,请检查您的流程并进行更改。
标签: ios objective-c boolean