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

时间:2015-10-22 10:39:22

标签: ios uibutton

在我的应用程序中,我必须使用单一方法在UIButton中插入两个UIView,同时显示弹出窗口。而且我已经完成了它,但是它总是将最后一个按钮添加到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 [最后一个按钮意味着最后一个按钮被添加为子视图]。

3 个答案:

答案 0 :(得分:1)

由于重叠,您正面临这个问题。它可以通过为两个按钮设置适当的坐标来解决。请找到附加的图像以获得更多的想法。

 - (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

由于

答案 1 :(得分:0)

我认为btnSearch涵盖btnManual。您可以为它们设置backgroundColor或在xcode中使用用户界面检查器。 检查titleLabelHeight值和

中的CGRectMake
_btnSearch=[[UIButton alloc]initWithFrame:CGRectMake(8, titleLabelHeight+20, popupView.frame.size.width - 16, popupView.frame.size.height - 16 - 40 - (titleLabelHeight + 16))];

screen

答案 2 :(得分:0)

为按钮创建一个常用方法,如下所示

[org.keycloak.adapters.OAuthRequestAuthenticator] failed verification of token: 
Token type is incorrect. Expected 'Bearer' but was 'null'

现在你要两次调用上面的函数,并添加两个带框的按钮,

按钮-(void)ButtonFrame:(CGRect)frame title:(NSString *)buttonTitle tag:(int)buttonTag { UIButton *button = [[UIButton alloc] initWithFrame:frame]; [button setTitle:buttonTitle forState:UIControlStateNormal]; [button setTitleColor: [UIColor colorWithRed:98.0f/255.0f green:42.0f/255.0f blue:101.0f/255.0f alpha:1] forState:UIControlStateNormal]; [button addTarget:self action:@selector(sendBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; [button setTag:buttonTag]; [popupView addSubview:button]; } 如下调用,

_btnManual

[self ButtonFrame:CGRectMake(8, titleLabelHeight-30, popupView.frame.size.width - 16, popupView.frame.size.height - 16 - 40 - (titleLabelHeight + 16)) title:@"Add Feed Manualy" tag:1]; 相同,如下所示,

_btnSearch

现在点击活动:

[self ButtonFrame:CGRectMake(8, titleLabelHeight+20, popupView.frame.size.width - 16, popupView.frame.size.height - 16 - 40 - (titleLabelHeight + 16)) title:@"Search Data From Feed" tag:2];