如何从另一个UIViewController向UIButtoun Outlet提供动作

时间:2014-07-17 06:35:38

标签: objective-c uiviewcontroller uibutton iboutlet

我有两个UIViewController课程,其名称为:RootViewController& SecondViewController)。 我的UIButton中有一个SecondViewController插座。现在我想在UIButton中为我的RootViewController提供操作方法。但我不知道。

请指导我并告诉我如何在另一个视图中获取UIButton并在另一个视图中给出操作方法....

4 个答案:

答案 0 :(得分:1)

我不确定你为什么这样做,它看起来并不好看。 无论如何,就在这里。

SecondViewController.h

@interface SecondViewcontroller:UIViewController
@property (nonatomic, weak) IBOutlet UIButton *theButton;
@end

RootViewController.m

SecondViewContoller * sv = [[SecondViewController alloc] init];
[sv.theButton addTarget:self action:@selector(buttonHandler:) forControlEvents:UIControlEventTouchUpInside];

完成按钮后,您必须删除目标。

为什么不使用委托或阻止回调?

<强> ADDED

<强> 委托

SecondViewController.h

@protocol SecondViewControllerDelegate <NSObject>
- (void)theButtonPressed;
@end

@interface SecondViewController : UIViewController
@property (nonatomic, strong) id<SecondViewControllerDelegate>delegate;
@end

SecondViewController.m

@interface SecondViewController ()
{
    UIButton *theButton;
}
@end

@implementation SecondViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [theButton addTarget:self.delegate action:@selector(theButtonPressed) forControlEvents:UIControlEventTouchUpInside];
}
@end

RootViewcontroller.h

@interface RootViewController : UIViewController <SecondViewControllerDelegate>
@end

RootViewController.m

SecondViewController *sv = [[SecondViewController alloc] init];
[sv setDelegate:self];

- (void)theButtonPressed
{
}

阻止

SecondViewController.h

typedef void(^TheButtonTouched)(void);

@interface SecondViewController : UIViewController
- (void)addButtonEvent:(TheButtonTouched)event;
@end

SecondViewController.m

@interface SecondViewController ()
{
    UIButton *theButton;
    TheButtonTouched buttonBlock;
}
@end

@implementation SecondViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // button alloc init here and..
    [theButton addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonEvent:(UIButton *)sender
{
    if(buttonBlock)
    {
        buttonBlock();
    }
}

- (void)addButtonEvent:(TheButtonTouched)event
{
    buttonBlock = event;
}
@end

RootViewController.m

SecondViewController *sv = [[SecondViewController alloc] init];
[sv addButtonEvent:^{
    // ADD SOMTHING HERE
}];

答案 1 :(得分:0)

只需创建RootViewController的Object并传入目标参数。

e.g。

[btnShow addTarget:self.rootCtrlObj action:@selector(viewPhoto_click:) 
forControlEvents:UIControlEventTouchUpInside]; 

其中rootCtrlObj是SecondViewController.h文件中的@property 当你在RootViewController中推送这个控制器时,你必须传递RootViewController的引用。

For Example,

secVCObj.rootCtrlObj = self;
[self.navigationController pushViewController:secVCObj animated:YES];

答案 2 :(得分:0)

您需要创建按钮的iboutlet属性。 比使用secondclass.button名称的对象,您可以指定选择器。

[classobjectname..btnname addTarget:objectname.class action:@selector(objectname.methodname:);

答案 3 :(得分:0)

正如我所说,自定义委托方法将适用于这些情况, 在SecondViewController.h文件中定义一个自定义委托方法,如下所示


#import <UIKit/UIKit.h>
#import "ViewController.h"
@protocol SecondControllerDelegate<NSObject>
- (void)someActionToPerformInRootController; 
@end

@interface SecondViewController : UIViewController
@property (nonatomic, assign) id<SecondControllerDelegate>delegate; //declare a custom delegate
@end


SecondViewController.m文件中的

可以执行某个按钮的操作

- (IBAction)myButtonActionMethod:(id)sender
{
  if([self.delegate respondsToSelector:@selector(someActionToPerformInRootController)])
   {
      [self.delegate someActionToPerformInRootController]; //this will call the delegate method in first view conroller( root view controller) //do what ever u want t do in the action method of rootviewcontroller
  }
}
<{1>}文件中的

RootViewController.h
#import "SecondViewController.h" @interface RootViewController : UIViewController < SecondControllerDelegate> //confirmas to delegate 中的{p> RootViewController.m同时将SecondViewController的代表设置为self

- (IBAction)showAction:(id)sender
{
    SecondViewController *secondController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
    secondController.delegate = self; //call back to this controller
    [self presentViewController:secondController animated:YES completion:nil];
}

//this method called from the secondViewcontroller
- (void) someActionToPerformInRootController
{
    NSLog(@"perform action in root view controller");
}