我试图将一些信息从模态视图发送到delgate。但似乎我的代表没有遵循,它变为空。它在IB http://i.imgur.com/7oaxb.png中看起来像这样。 但是如果我删除了模态视图之前的navigationController并使用视图中的按钮,它就可以工作。 请帮助,我已经尝试了5个小时......:/
继承modalViewController代码: #进口 #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];
}
答案 0 :(得分:6)
如果目标视图控制器被包装到导航控制器中,则必须在prepareForSegue
中以不同方式引用它:
UINavigationController *nav = segue.destinationViewController;
DetailViewController *dvc = [nav.viewControllers objectAtIndex:0];
现在设置属性(包括委托)应该可以正常工作。