1

我试图将一些信息从模态视图发送到委托人。但似乎我的代表没有贯彻执行,它变成了空值。在 IB http://i.imgur.com/7oaxb.png中看起来像这样。 但是,如果我删除模态视图之前的 navigationController 并使用视图中的按钮,它会起作用。 请帮忙,我尝试了 5 个小时... :/

下面是 modalViewController 代码:#import #import "Link.h"

@protocol modalViewDelegate <NSObject>

-(void)closeview;
-(void)saveLink:(Link *)link;

@end

@interface modelViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *titel;
@property (weak, nonatomic) IBOutlet UITextField *url;

@property (nonatomic, weak) id <modalViewDelegate> delegate;

- (IBAction)exitModal:(id)sender;
- (IBAction)saveLink:(id)sender;

@end

和.m:

#import "modelViewController.h"

@interface modelViewController ()

@end

@implementation modelViewController
@synthesize titel;
@synthesize url, delegate;

- (IBAction)exitModal:(id)sender {
    //[self.delegate closeview];
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)saveLink:(id)sender {
    if (titel.text.length > 0 && url.text.length > 0) {

        NSString *urlen = [NSString stringWithFormat:@"%@", url.text];

        Link *linken = [[Link alloc] initWithURL:[NSURL URLWithString:urlen]];
        linken.title = titel.text;

        NSLog(@"%@", delegate); **//returns null when pressing button** it returns null if i put it in viewDidLoad to..

        [self.delegate saveLink:linken];

        [self dismissModalViewControllerAnimated:YES];



    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"warning" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alertView show];
    }
}
@end

MasterViewController .h(推动模态视图:

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

@class DetailViewController;

@interface MasterViewController : UITableViewController <modalViewDelegate>
....

和.m

#import "MasterViewController.h"
#import "DetailViewController.h"

@implementation MasterViewController

@synthesize detailViewController = _detailViewController;
@synthesize links;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"linkPush"]) {
        // Skicka länken till detaljvyn
        DetailViewController *detailVC = segue.destinationViewController;
        detailVC.link = [self.links objectAtIndex:self.tableView.indexPathForSelectedRow.row];
        NSLog(@"%@", detailVC);
    }

    //this is the modalview "pusher"
    if ([segue.identifier isEqualToString:@"newLink"]) {

        modelViewController *mvc = segue.destinationViewController;
        mvc.delegate = self;
        NSLog(@"%@", mvc.delegate);
    }


}

- (void)closeview {
    [self dismissModalViewControllerAnimated:YES];
    //[self.tabBarController dismissModalViewControllerAnimated:YES];
}

-(void)saveLink:(Link *)link{

    NSLog(@"hello");
    [links insertObject:link atIndex:links.count]; //updates a Tableview and works fine if delegate is called

    [self.tableView reloadData];
    //[self dismissModalViewControllerAnimated:YES];
}
4

1 回答 1

6

如果您的目标视图控制器被包装到导航控制器中,则您必须在以下内容中以不同方式引用它prepareForSegue

UINavigationController *nav = segue.destinationViewController;
DetailViewController *dvc = [nav.viewControllers objectAtIndex:0];

现在设置属性,包括委托,应该可以工作了。

于 2012-09-28T13:50:56.257 回答