0

我有一个使用默认模板主详细信息制作的应用程序。我想将一个字符串从详细视图传递到主视图,点击一个按钮。我使用了这段代码:

细节视图控制器.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

细节视图控制器.m

-(IBAction)passBack:(id)sender
{
     itemToPassBack = [[NSString alloc] initWithFormat:@"PassedBack"];


     [[self delegate] addItemViewController:self didFinishEnteringItem: itemToPassBack];
     [self.navigationController popViewControllerAnimated:YES];
 }

主视图控制器.h

 @interface MasterViewController : UITableViewController <DetailViewControllerDelegate>

 @property (nonatomic) NSString *returnedItem;
 @end

主视图控制器.m

 -(void) passItemToMaster:(id) controller didFinishEnteringItem: (NSString *)item
 {
      returnedItem = item;
      NSLog(@"This was returned from secondPageViewController");

 }

如何在主视图中获取对象?nslog 不显示任何内容。

4

3 回答 3

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

我不知道在哪里定义委托。

于 2013-09-28T08:03:08.780 回答
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];
    }
}
于 2013-09-27T20:48:43.620 回答
0

您可以像这样获得委托对象/属性:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

appDelegate.object;
[appDelegate property];
于 2013-09-27T23:09:07.507 回答