从另一个视图控制器调用一个视图控制器的方法

发布于 2024-12-11 17:06:16 字数 952 浏览 1 评论 0原文

方法“someMethod”,

@interface OneViewController
{
UIView *tempView;
..

}
-(void) someMethod ;
@end

我有一个在 OneViewController.h 中声明并在 OneViewController.m 文件中实现的

@implementation OneViewController

-(void) someMethod 
{
tempView = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 200, 250)];
tempView.backgroundColor = [UIColor yellowColor];
if([[self.view subviews] containsObject:tempView])
[tempView removeFromSuperView];
   else
   [self.view addsubview:tempView];

}

当出现在不同的 viewController - secondaryViewController 时,我想调用 someMethod

(类似于 [OneViewController someMethod]),这样当我返回 OneViewController 时,我可以看到 someMethod 所做的更改。

我需要使用 appDelegate 方法吗?

我已经尝试遵循但它不起作用。

neViewController *newViewController = [[OneViewController alloc] init];
[newViewController someMethod];

感谢您提前提供任何帮助..

I have a method "someMethod" declared in OneViewController.h

@interface OneViewController
{
UIView *tempView;
..

}
-(void) someMethod ;
@end

and implemented in OneViewController.m file

@implementation OneViewController

-(void) someMethod 
{
tempView = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 200, 250)];
tempView.backgroundColor = [UIColor yellowColor];
if([[self.view subviews] containsObject:tempView])
[tempView removeFromSuperView];
   else
   [self.view addsubview:tempView];

}

I want to call someMethod when present at different viewController - secondViewController

(something like [OneViewController someMethod]), So that when I get back to OneViewController I can see the changes made by someMethod.

Do I need to use appDelegate methods?

I have tried following but it doesn't work.

neViewController *newViewController = [[OneViewController alloc] init];
[newViewController someMethod];

Thanks for any help in advance..

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

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

发布评论

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

评论(4

源来凯始玺欢你 2024-12-18 17:06:16

在 SecondViewController 中,声明 OneViewController 类的引用。您可以分配财产。在移动到 SecondViewController 之前设置引用。现在有了引用,您就可以调用实例方法[_oneView someMethod]

编辑:

声明

OneViewController *_oneView;

同时添加分配属性,

@property(nonatomic,assign) OneViewController *_oneView;

在.m文件中综合变量。

从 OneViewController 显示 SecondViewController 时,只需添加以下行。

secondView._oneView = self;

In the SecondViewController, declare a reference for OneViewController class. You can have assign property. Set the reference before you move to SecondViewController. Now with the reference, you can call the instance method [_oneView someMethod].

Edit:

Declare

OneViewController *_oneView;

Also add the assign property,

@property(nonatomic,assign) OneViewController *_oneView;

Synthesize the variable in .m file.

While showing the SecondViewController from OneViewController, just add the following line.

secondView._oneView = self;
淡看悲欢离合 2024-12-18 17:06:16

有时直接调用创建[classObject methodName]的方法不会反映视图中的更改。就像如果您想将 UIScrollView 属性从 scrollEnble = NO; 更改为 scrollEnable = YES; 一样,它不会反映。
您应该使用 UIApplication 的单例。

假设您想在 ViewController2 中调用 ViewController1 的方法 - (void)myMethod 那么步骤如下:

  • 在您的 AppDelegate 中导入 ViewController1 并创建其对象 *vc。 Delare 属性@property(强、非原子)ViewController1 *vc;。也合成
  • 现在来到Viewcontroller1类。在 Viewcontroller1 中导入 AppDelegate.h 并在 viewDidLoad 中编写如下:

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

  • 转到您的 ViewController2.h 并导入 AppDelegate.h
  • 转到您要调用的行 ViewController2的方法并这样写:

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[appDelegate vc1] myMethod]; // 允许滚动

sometimes calling a method directlry creating [classObject methodName] does not refelect the changes in views. Like if you want to change a UIScrollView property from scrollEnble = NO; to scrollEnable = YES;, it does not refelect.
You should use singleton of UIApplication.

Suppose you want to call ViewController1's method - (void)myMethod in ViewController2 then here are the steps:

  • In you AppDelegate import ViewController1 and create its object *vc. Delare property @property (strong, nonatomic) ViewController1 *vc;. synthesize also.
  • Now come to Viewcontroller1 class. Import AppDelegate.h in you Viewcontroller1's and in viewDidLoad write like this:

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

  • Go to your ViewController2.h and import AppDelegate.h
  • Go to the line where you want to call ViewController2's method and write like this:

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[appDelegate vc1] myMethod]; // to allow scrolling

痴情换悲伤 2024-12-18 17:06:16

理想情况下,您应该创建一个协议并委托方法来完成您正在寻找的任务。

创建一个协议,在 secondaryViewController 中实现它,并将协议委托设置为firstViewController,然后使用委托方法调用 secondaryViewController 中的相关方法

我希望它对你有用..!!

Ideally, you should create a protocol and delegate methods to accomplish what you are looking for.

Create a protocol, implement it in secondViewController and set the protocol delegate to firstViewController and then use the delegate methods for invoking the relevant methods in secondViewController

I hope it works for you..!!

谜泪 2024-12-18 17:06:16

一种方法是在 OneViewController.h 中将声明更改为 +(void) someMethod ; ,并在实现文件中相应地将减号更改为加号。这将使其成为类方法而不是实例方法。然后在 SecondViewController.m 文件中,确保将 @class OneViewController; 放在实现声明之前;然后你可以调用 [OneViewController someMethod] 它应该执行。干杯!

One way of doing it is to change the declaration to +(void) someMethod ; in your OneViewController.h and change the minus to a plus correspondingly in the implementation file. This will make it a class-method and not an instance method. Then in your SecondViewController.m file, make sure to put @class OneViewController; before the implementation declaration; then you can call [OneViewController someMethod] and it should execute. Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文