如何将函数一个类访问另一个目标c

时间:2013-10-15 04:40:40

标签: ios objective-c

这是我的viewcontroller1.m按钮userinteractionenabled函数,

-(void) BtnUserInteractionStatus {
   oneBtn2.userInteractionEnabled = NO; 
   oneBtn3.userInteractionEnabled = NO;
}

- (void)viewDidLoad {
   [super viewDidLoad];
   [self BtnUserInteractionStatus];
}

这是我的viewcontroller2.m按钮来自viewcontroller1.m的userinteractionenabled函数访问,

#import "ViewController1.h"

- (void)viewDidLoad {
   [super viewDidLoad];

   svc = [[StageViewController alloc]init];
   [svc BtnUserInteractionStatus];
}

我的viewcontroller2.h,

#import "ViewController1.h"

@interface ViewController2 : UIViewController {
StageViewController *svc;
}

@property (assign) StageViewController *svc;

但是,这不起作用。如果有任何错误请更正。

1 个答案:

答案 0 :(得分:1)

这将是您的ViewController2.m

  #import "ViewController1.h"

- (void)viewDidLoad 
 {
    [super viewDidLoad];

    svc = [[ViewController1 alloc]initWithNibName:@"ViewController1" bundle:nil];
    [self.view addSubview:svc.view];
}

ViewController2.h

    #import "ViewController1.h"

   @interface ViewController2 : UIViewController 
   {
          ViewController1 *svc;
   }

ViewController1.m

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self)
    {
    // Custom initialization

     [self BtnUserInteractionStatus];    /*Provide your call to BtnUserInteractionStatus within init function*/

   }
   return self;
 }




 - (void)viewDidLoad
  {
      [super viewDidLoad];
     // Do any additional setup after loading the view from its nib.


  }


   -(void)BtnUserInteractionStatus
     {

         oneBtn2.userInteractionEnabled = NO; 
         oneBtn3.userInteractionEnabled = NO;



     }

每当你想从另一个ViewContoller加载一个viewController的对象库元素时,你需要定义它的xib然后将它的相应视图添加为当前viewcontroller的子视图或者从当前的ViewController推送viewcontrller。只需调用BtnUserInteractionStatus使用当前创建的实例的方法不会帮助..

希望它能帮助!!