我有一个使用默认模板master-detail制作的应用程序。我想将一个字符串从详细视图传递到主视图,点击一个按钮。我用了这段代码:
Detailviewcontroller.h
@protocol DetailViewControllerDelegate <NSObject>
- (void)passItemToMaster:(id)controller didFinishEnteringItem:(NSString *)item;
@end
@interface DetailViewController : UIViewController<UITextFieldDelegate>
{
NSString *itemToPassBack;
}
@property (copy) NSString *itemToPassBack;
@property (retain) id delegate;
@end
Detailviewcontroller.m
-(IBAction)passBack:(id)sender
{
itemToPassBack = [[NSString alloc] initWithFormat:@"PassedBack"];
[[self delegate] addItemViewController:self didFinishEnteringItem: itemToPassBack];
[self.navigationController popViewControllerAnimated:YES];
}
Masterviewcontroller.h
@interface MasterViewController : UITableViewController <DetailViewControllerDelegate>
@property (nonatomic) NSString *returnedItem;
@end
Masterviewcontroller.m
-(void) passItemToMaster:(id) controller didFinishEnteringItem: (NSString *)item
{
returnedItem = item;
NSLog(@"This was returned from secondPageViewController");
}
如何在主视图中获取对象? nslog没有显示任何内容。
答案 0 :(得分:0)
从masterViewController创建DetailView时,请添加以下行:
_detailViewController.delegate = self;
将prepereForSegue
替换为:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSString *object = _nameField.text;
DetailViewController *detailViewController = segue.destinationViewController
detailViewController.delegate = self;
[[segue destinationViewController] setDetailItem:object];
}
}
答案 1 :(得分:0)
您可以获得这样的委托对象/属性:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.object;
[appDelegate property];
答案 2 :(得分:0)
感谢您的回答。假设我是Xcode编程的新手,我必须了解更好的代表。我使用了模板,我修改了一些东西。所以我有这个 masterviewcontroller.m
@interface MasterViewController () {
NSMutableArray *_objects;
}
@end
@implementation MasterViewController
@synthesize returnedItem;
-(void) addItemViewController:(id) controller didFinishEnteringItem: (NSString *)item
{
returnedItem = item;
NSLog(@"This was returned from secondPageViewController");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
//create new cell
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
CGRect frame = CGRectMake (68, 12, 225, 22);
UITextField *textfield = [[UITextField alloc] initWithFrame:frame];
[self setNameField:textfield];
[_nameField setDelegate:self];
[_nameField setReturnKeyType:UIReturnKeyDone];
[_nameField addTarget:self
action:@selector(textFieldFinished:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.contentView addSubview: _nameField];
cell.textLabel.enabled = YES;
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (void)textFieldFinished:(id)sender{
[sender resignFirstResponder];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *title = [_objects objectAtIndex: [fromIndexPath row]];
[_objects removeObjectAtIndex:[fromIndexPath row]];
[_objects insertObject:title atIndex:[toIndexPath row]];
[[self tableView] reloadData];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSString *object = _nameField.text;
[[segue destinationViewController] setDetailItem:object];
}
}
@end
我不知道在哪里定义委托。