在 iOS 中从 ParentViewController 调用 SEL

发布于 2024-12-02 15:45:41 字数 453 浏览 1 评论 0原文

我有一个 ViewController,在其中创建了一个模态 ViewController。我的模态中有一个 SEL 值,是在从父级实例化它时设置的。

setDateViewController.selectorName = @selector(myMethod:);

在我的模态中,我尝试将此 SEL 称为:

[[self parentViewController] performSelector:self.selectorName withObject:selectedDate afterDelay:.5]; 

{selectedDate} 显然是我的模态中的值。

我没有收到任何错误或堆栈,但是,我的父级上的这个 SEL(方法)从未被调用。出于某种原因,我认为这应该可行,但有件事告诉我我已经偏离了轨道。

谢谢。

I have a ViewController in which I create a Modal ViewController. I have a SEL value in my modal that I set when it is being instantiated from the parent.

setDateViewController.selectorName = @selector(myMethod:);

In my modal I am trying to call this SEL like:

[[self parentViewController] performSelector:self.selectorName withObject:selectedDate afterDelay:.5]; 

{selectedDate} is obviously a value from my modal.

I don't get any errors or stack, however, this SEL (method) on my parent is never being called. For some reason I think this should work, but something tells me I'm way off track.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书间行客 2024-12-09 15:45:41

我猜 [selfparentviewcontroller] 没有返回任何内容。
尝试 UiviewController* v = [selfparentviewcontroller];并检查是否为零。最有可能它应该是零。否则,如果它指向不同类的另一个对象,那么它就会崩溃。请做一件事。您应该将 bot 设置为您需要调用的对象和方法。如果有任何问题,IT 部门都会解决。

setDateViewController.selectorDelegate = self;
setDateViewController.selectorName = @selector(myMethod:);

从父类中这样调用。因此您可以动态指定要调用的方法和对象,从而提供了更大的灵活性。

并使用,
[selectorDelegate PerformSelector:self.selectorName withObject:selectedDate afterDelay:.5];

这应该可以解决任何问题。

I guess [self parentviewcontroller] is not returning anything.
Try UiviewController* v = [self parentviewcontroller]; and check if is nil. Most probably it should be nil. Else if its was pointing to another object of different class then it would have crashed. Please do one thing. YOu should set bot the object and the methd you need to call. IT will solve any issues if it has any.

setDateViewController.selectorDelegate = self;
setDateViewController.selectorName = @selector(myMethod:);

call like this from parent class. So you can dynamically specify the method and the object you want to call gives more flexibility.

and use,
[selectorDelegate performSelector:self.selectorName withObject:selectedDate afterDelay:.5];

this should solve any issues.

呢古 2024-12-09 15:45:41

也许您会考虑向您的模式添加委托协议,以允许它调用父级上的方法。

快速(未经测试)示例:

// MyController.h
@protocol MyControllerDelegate;
@interface MyController : UIViewController
{
    id<MyControllerDelegate> delegate;
}
@end

@protocol MyControllerDelegate <NSObject>
 - (void)methodToCall:(id)sender;
@end


// MyControler.m
@implementation MyController

- (void) loadView
{
    [super loadView];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20.0f, 20.0f, 50.0f, 30.0f);
    [btn setTitle:@"blah" forState:UIControlStateNormal];
    [btn addTarget:self.delegate 
         action:@selector(methodToCall:) 
         forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];
}

@end

// ParentController.h
@interface ParentController : UIViewController<MyControllerDelegate>
{
}
@end

// ParentController.m
@implementation ParentController

- (void)methodToCall:(id)sender
{
    NSLog(@"HERE");
}

@end

只需确保在创建模态控制器时将其委托设置为父级上的 self :

MyController *controller = [[MyController alloc] init];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];

Perhaps you would consider added a delegate protocol to your modal that will allow it to call the method on the parent.

Quick (untested) example:

// MyController.h
@protocol MyControllerDelegate;
@interface MyController : UIViewController
{
    id<MyControllerDelegate> delegate;
}
@end

@protocol MyControllerDelegate <NSObject>
 - (void)methodToCall:(id)sender;
@end


// MyControler.m
@implementation MyController

- (void) loadView
{
    [super loadView];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20.0f, 20.0f, 50.0f, 30.0f);
    [btn setTitle:@"blah" forState:UIControlStateNormal];
    [btn addTarget:self.delegate 
         action:@selector(methodToCall:) 
         forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];
}

@end

// ParentController.h
@interface ParentController : UIViewController<MyControllerDelegate>
{
}
@end

// ParentController.m
@implementation ParentController

- (void)methodToCall:(id)sender
{
    NSLog(@"HERE");
}

@end

Just make sure when you are creating your modal controller you set it's delegate to self on the parent:

MyController *controller = [[MyController alloc] init];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文