我有以下代码从名为FirstViewController的方法文件生成UIBUtton,因为按钮的位置将在不同的secondviewController或thirdViewController中更改,是否可以为解锁的位置(CGRect)设置变量FirstViewController并在第二个或第三个viewController中更改CGRect值?
在FirstViewController.m中
struct node
{
int data;
struct node *left, *right;
};
在SecondViewController.m中
-(void)method:(UIView *)_view {
UIButton*Touch1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[Touch1 addTarget:self action:@selector(TouchButton1:) forControlEvents:UIControlEventTouchUpInside];
[Touch1 setFrame:CGRectMake(50,50, 100, 100)];
**//I want to set a variable for the CGRectMake**
Touch1.translatesAutoresizingMaskIntoConstraints = YES;
[Touch1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[Touch1 setExclusiveTouch:YES];
[_view addSubview:Touch1];
NSLog(@"test ");
}
答案 0 :(得分:1)
此模式不正确:
ViewController * ViewCon = [[ViewController alloc]init];
[ViewCon method:self.view];
因为你正在分配一个新的视图控制器只是为了使用它的一个方法然后你扔掉视图控制器实例。它非常低效且不方便。
将方法移动到实用程序类,作为类方法:
[Utils method:self.view rect:rect];
或子类UIViewController
并在该基类中实现该方法,然后从该基类派生所有类似的视图控制器,并将任何变量传递给它。
[self method:rect]; // method implemented in MyBaseViewController
您还问了question before,并接受了一个促使使用这种不良模式的答案。这将误导其他人使用这种糟糕的模式。