在我的应用程序中,当弹出窗口出现时,我必须使用单一方法在 UIView
中插入两个 UIButton
。我已经做到了,但这样它总是调用添加到 subView
的最后一个按钮。
这是我的代码:-
int titleLabelHeight = titleLabel.frame.size.height;
_btnManual=[[UIButton alloc]initWithFrame:CGRectMake(8, titleLabelHeight-30, popupView.frame.size.width - 16, popupView.frame.size.height - 16 - 40 - (titleLabelHeight + 16))];
[_btnManual addTarget:self
action:@selector(sendBtnPressed:)
forControlEvents:UIControlEventTouchUpInside];
[_btnManual setTitle:@"Add Feed Manualy" forState:UIControlStateNormal];
[_btnManual setTag:1];
[_btnManual setTitleColor: [UIColor colorWithRed:98.0f/255.0f green:42.0f/255.0f blue:101.0f/255.0f alpha:1] forState:UIControlStateNormal];
[popupView addSubview:_btnManual ];
_btnSearch=[[UIButton alloc]initWithFrame:CGRectMake(8, titleLabelHeight+20, popupView.frame.size.width - 16, popupView.frame.size.height - 16 - 40 - (titleLabelHeight + 16))];
[_btnSearch addTarget:self
action:@selector(sendBtnPressed:)
forControlEvents:UIControlEventTouchUpInside];
[_btnSearch setTag:2];
[_btnSearch setTitle:@"Search Data From Feed" forState:UIControlStateNormal];
[_btnSearch setTitleColor: [UIColor colorWithRed:98.0f/255.0f green:42.0f/255.0f blue:101.0f/255.0f alpha:1] forState:UIControlStateNormal];
[popupView addSubview:_btnSearch ];
-(IBAction)sendBtnPressed:(UIButton*)sender
{
NSLog(@"==%@",sender);
if(sender == _btnManual)
{
searchfromfeed1=NO;
[_btnManual setHidden:YES];
[_btnSearch setHidden:YES];
[self setupSubtitle];
[self setuptableview];
[self setupTextFields];
[self setupButtons];
}
else if (sender == _btnSearch)
{
searchfromfeed1 = YES;
[_btnManual setHidden:YES];
[_btnSearch setHidden:YES];
//[self setupSubtitle];
[self setuptableview];
[self setupTextFields];
[self setupButtons];
}
}
但是这样它总是调用sender = tag2
[最后一个按钮意味着最后一个按钮添加为 subview ]。
最佳答案
你因为重叠而面临这个问题。这可以通过为两个按钮设置适当的坐标来解决。请查看附件图像以获得更多想法。
- (void)viewDidLoad {
[super viewDidLoad];
_btnManual=[[UIButton alloc]initWithFrame:CGRectMake(8, 0, self.view.frame.size.width - 16, 100)];
_btnManual.backgroundColor=[UIColor blueColor];
[_btnManual addTarget:self
action:@selector(sendBtnPressed:)
forControlEvents:UIControlEventTouchUpInside];
[_btnManual setTitle:@"Add Feed Manualy" forState:UIControlStateNormal];
[_btnManual setTag:1];
[_btnManual setTitleColor: [UIColor colorWithRed:98.0f/255.0f green:42.0f/255.0f blue:101.0f/255.0f alpha:1] forState:UIControlStateNormal];
[self.view addSubview:_btnManual ];
_btnSearch=[[UIButton alloc]initWithFrame:CGRectMake(8, _btnManual.frame.size.height+10, self.view.frame.size.width - 16, 100)];
_btnSearch.backgroundColor=[UIColor yellowColor];
[_btnSearch addTarget:self
action:@selector(sendBtnPressed:)
forControlEvents:UIControlEventTouchUpInside];
[_btnSearch setTag:2];
[_btnSearch setTitle:@"Search Data From Feed" forState:UIControlStateNormal];
[_btnSearch setTitleColor: [UIColor colorWithRed:98.0f/255.0f green:42.0f/255.0f blue:101.0f/255.0f alpha:1] forState:UIControlStateNormal];
[self.view addSubview:_btnSearch ];
//[self.view addSubview:_popupView];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)sendBtnPressed:(UIButton*)sender
{
if(sender == _btnManual)
{
[_btnManual setHidden:YES];
[_btnSearch setHidden:YES];
}
else if (sender == _btnSearch)
{
[_btnManual setHidden:YES];
[_btnSearch setHidden:YES];
//[self setupSubtitle];
}
}
谢谢
关于iOS 如何以编程方式在单个方法中插入两个 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33279069/