gpt4 book ai didi

ios - 将属性从模态视图 Controller 传递给父级

转载 作者:行者123 更新时间:2023-12-01 17:20:30 24 4
gpt4 key购买 nike

我有一个 tableview 作为父 View Controller 和一个子模态视图 Controller 。在模态视图 Controller 中,当用户点击一行时,我想设置父级的属性“过滤器”。但是,它只是返回 null。

如何将 NSString 过滤器属性传递回其父 View ?我应该在 didSelectRowAtIndexPath 方法中实例化父 View Controller 吗?

更新:使用 Delegates 解决,后跟 this tutorial .

下面是模态视图 Controller 的代码:

#import "FilterViewController.h"
#import "ContactsTableViewController.h"


@interface FilterViewController ()

@end

@implementation FilterViewController


- (void)viewDidLoad
{
[super viewDidLoad];
self.filterTable.dataSource = self;
self.filterTable.delegate = self;
[self performSelector:@selector(retrieveFilteredEvents)];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.filterEvents count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"filterTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


NSDictionary *tempDict = [self.filterEvents objectAtIndex:indexPath.row];

self.eventTitle = [tempDict objectForKey:@"eventType"];
cell.textLabel.text = self.eventTitle;

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *tempDict = [self.filterEvents objectAtIndex:indexPath.row];
NSString *string = [tempDict objectForKey:@"eventType"];

ContactsTableViewController *contactVC = [[ContactsTableViewController alloc] init];
contactVC.filter = string;

[self dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateParent" object:nil];
}

#pragma mark - Helper Methods

- (IBAction)done:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}

-(void)retrieveFilteredEvents
{
PFQuery *retrieveEvents = [PFQuery queryWithClassName:@"eventTypes"];
[retrieveEvents orderByAscending:@"eventOrder"];
[retrieveEvents findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.filterEvents = [[NSArray alloc] initWithArray:objects];
}
[self.filterTable reloadData];
}];

}

@end

最佳答案

有多种方法可以解决这个问题。我认为最简单的方法是在您的父类中设置一个属性,该属性会在您每次更新子类时更改。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *tempDict = [self.filterEvents objectAtIndex:indexPath.row];
NSString *string = [tempDict objectForKey:@"eventType"];

self.parentViewController.filter = string;

[self dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateParent" object:nil];
}

另一种选择是使用代表。这可能是一个更清洁的解决方案,但对初学者不友好。

http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

关于ios - 将属性从模态视图 Controller 传递给父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24100538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com