iOS 如何以编程方式在单个方法中插入两个 UIButton?

标签 ios uibutton

在我的应用程序中,当弹出窗口出现时,我必须使用单一方法在 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];

        }
    }

enter image description here enter image description here

谢谢

关于iOS 如何以编程方式在单个方法中插入两个 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33279069/

相关文章:

ios - 选择和取消选择时设置按钮图像

ios - 如何禁用 UIButton 并发送 UIAlertView 消息

iphone - 如何创建椭圆形UIButton

ios - UITableViewCell didSelectRowAt : not getting called when press on UIButton area in cell

ios - 如何为基于 autotool 的项目启用特定于 Xcode 的构建标志

ios - 当用户将焦点从文本字段更改为另一个时调用方法 (RubyMotion)

与从 IDE 构建/运行相比,iOS 14 不允许 Flutter 应用程序(仍处于开发阶段)从主屏幕启动

iphone - 我如何从另一个方法调用这个方法?

ios - 在ios上将东距和北距转换为纬度和经度

ios - 为表格 View 中的单元格而不是其他单元格关闭画外音焦点