委托不调用协议方法

时间:2016-04-19 06:31:58

标签: ios objective-c iphone ipad delegates

我已经在我的应用中实现了协议,

我在line = "command[check_hda]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sdb" line.gsub("/dev/sdb", "/dev/xvda1") #=> "command[check_hda]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/xvda1" 中声明了协议方法,我从File.open("/your/file", "r") { |file| file.each_line { |line| puts line.gsub("/dev/sdb", "/dev/xvda1") }}

呼叫loginViewController

这是我的代码段: 的 protocolMethod

confirmViewController

loginViewController.h

@protocol FirstControllerDelegate<NSObject>
@required
-(void)protocolMethod;

@end

@interface loginViewController : UIViewController<FirstControllerDelegate>

@end

代码 的 loginViewController.m

- (IBAction)loginBtnClicked:(id)sender {
    confirmViewController *obj = [confirmViewController new];
     obj.delegate=self;
   [self performSegueWithIdentifier:@"confirmloginsegue" sender:self];


}

-(void)protocolMethod{
    NSLog(@"--- This is not callled ---");
     [self dismissViewControllerAnimated:NO completion:nil];
}

confirmViewController.h

@interface confirmViewController : UIViewController

@property (nonatomic, assign) id <FirstControllerDelegate> delegate;

@end

我在哪里做错了?

请提前帮助和致谢!

6 个答案:

答案 0 :(得分:2)

错误的方法和委托初始化

在这一行: -

confirmViewController *obj = [confirmViewController new];
     obj.delegate=self;
   [self performSegueWithIdentifier:@"confirmloginsegue" sender:self];    

--- You are loosing the object here

解决方案

在属性检查器中给出confirmViewController的标识符,现在按下vc并设置对象confirmViewController的委托,如下所示: -

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ConfirmViewController *confirmViewController = [storyboard instantiateViewControllerWithIdentifier:@"ConfirmViewController"];

confirmViewController.delegate=self;

[self.navigationController pushViewController:confirmViewController animated:YES];

答案 1 :(得分:0)

委托类的存在时间不够长:

- (IBAction)loginBtnClicked:(id)sender {
    confirmViewController *obj = [confirmViewController new];
    obj.delegate=self;
    [self performSegueWithIdentifier:@"confirmloginsegue" sender:self];
    // obj is destroyed here
}

答案 2 :(得分:0)

您需要实施export default class FilterButton extends React.Component { constructor(props) { super(props); } componentWillMount() { } render() { return ( <button onClick={this.props.onClick} className={'btn btn-filter btn-sm'+(this.props.active ? ' active' : '')}> <span className="filter-name">{this.props.name}</span><span className="filter-count">{this.props.count}</span> </button> ) } } FilterButton.contextTypes = { executeAction: React.PropTypes.func.isRequired }; 并在那里设置protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); builder.Entity<Post>().HasRequired(p => p.Blog); }

prepareForSegue

有关详细信息,请查看此链接: - http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

答案 3 :(得分:0)

你必须在那里设置委托。请在代码中进行一些修正,例如:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
confirmViewController *destViewController = (confirmViewController *)segue.destinationViewController;
destViewController.delegate=self;

}

答案 4 :(得分:0)

您应该使用preparefrosegue来设置您的委托对象。所以你的代码应该是这样的,

- (IBAction)loginBtnClicked:(id)sender {

   [self performSegueWithIdentifier:@"confirmloginsegue" sender:self];
  }

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

confirmViewController *obj = [confirmViewController new];

//or

confirmViewController *obj = [segue destinationViewController];

obj.delegate=self;
}

希望这会有所帮助;)

答案 5 :(得分:0)

如果您使用的是View控制器

SettingsHubViewController *settingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"yourStoryBoardIdentifier"];

settingVC.delegate = self;

如果您正在使用XIB,请在设置settingVC.delegate = self;

之后加载NIB。

它为我工作(我创建了OverLay视图并加载到主类中)