簡體   English   中英

使用UIButton將信息發送到void方法

[英]Send information with UIButton to void method

我正在創建帶有for循環的大量按鈕,並且希望在單擊按鈕時從一組數據中發送數據

我的按鈕像這樣在for循環中

標題正在for循環中處理,而Url是標題關鍵字的字典

-(void)addToScrollView {

     UIColor *ezBlue = [UIColor colorWithRed:(17.0 / 255.0) green:(95.0 / 255.0) blue:(207.0 / 255.0) alpha: 1];
    UIColor *ezGreen = [UIColor colorWithRed:(57.0 / 255.0) green:(145.0 / 255.0) blue:(2.0 / 255.0) alpha: 1];


    yLoc = 0;
    for(NSString *titleString in titlesArray) {

        int bufferInt = 2;
        buttonHeight = 0;
        buttonTop = yLoc;

        NSString *descString = [dFT objectForKey:titleString];
        NSString *dispUrlString = [duFT objectForKey:titleString];
        NSString *urlString = [uFT objectForKey:titleString];

        ...
        ...

        UIButton *buttonToPage = [[UIButton alloc] initWithFrame:CGRectMake(20, buttonTop, 280, buttonHeight)];
        [buttonToPage addTarget:self action:@selector(customBrowser:) forControlEvents:UIControlEventTouchUpInside];

        [mainScrollView addSubview:buttonToPage];

    }
    height = yLoc;
    [self scrollViewHeight];
}

以及應該將數據發送到的void方法

-(void)customBrowser:(NSString *)url {
//Do Stuff here
}

如何將諸如url之類的信息傳遞給void方法,以便可以對其進行處理。

可能您有一個包含URL或類似內容的數組,對嗎?

這不是最好的方法,但是您可以使用按鈕中的“ tag”屬性來指示URL的索引。

-(void)customBrowser:(UIButton *)sender
{
   NSString *stringURL = self.arrayURL[sender.tag];
   //do your stuff here
}

但是,我建議您嘗試使用TableViews或CollectionViews解決您的問題。

最終將按鈕的標題設置為url並以類似方法讀取它。

設定...

UIButton *buttonToPage = [[UIButton alloc] initWithFrame:CGRectMake(20, buttonTop, 280, buttonHeight)];
        [buttonToPage setTitle:urlString forState:UIControlStateNormal];
        [buttonToPage addTarget:self action:@selector(customBrowser:) forControlEvents:UIControlEventTouchUpInside];

        [mainScrollView addSubview:buttonToPage];

閱讀它...

-(void)customBrowser:(UIButton *)sender {
//Do Stuff here
NSLog(@"The button title is %@",sender.titleLabel.text);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM