2014-10-20 3 views
0

Я создаю несколько кнопок динамически, теперь я хочу установить действие для каждой кнопки. , когда я нажимаю кнопки, показывает результат его отображения на следующем экране в моем приложении. Я использую библиотеку FMDB для чтения данных из базы данных (читайте базу данных в соответствии с идентификатором на кнопках нажмите). Я знаю, как читать базу данных из базы данных. я хочу только получать данные на кнопках, нажимать и отображать, что данные в виде таблицы. Как создать действие для каждой кнопки? это мой код кнопки:Как создать несколько динамических кнопок в ios?

-(void)DynamicButton:(NSMutableArray*)objectName 
    { 
    for(UIView *view in [scrollView subviews]) 
     { 
     [view removeFromSuperview]; 
     } 
     int yPossion = 100, xPossion = 44; int temp = 0; 


     for (int i = 0; i<[objectName count]; i++) 
    { 
    SMSCategory *cat = [objectName objectAtIndex:i]; 

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [aButton setTag:i]; 
    [aButton setTranslatesAutoresizingMaskIntoConstraints:YES]; 
    [aButton setBackgroundColor:[UIColor blackColor]]; 
    [aButton setBackgroundImage:[UIImage imageNamed:@"icon-menu.png"] 
    forState:UIControlStateNormal]; 

    [aButton setTitle:[NSString stringWithFormat:@"%d",i] 
    forState:UIControlStateNormal]; 

    [aButton setFrame:CGRectMake(xPossion, yPossion, 70, 60)]; 
    aButton.highlighted=YES; 

    [scrollView addSubview:aButton]; 

    ; 

    xPossion += aButton.frame.size.width+35; 
    temp++; 
    if (temp==3) 
    { 
     yPossion = aButton.frame.origin.y+aButton.frame.size.height+20; 
     temp = 0; 
     xPossion = 44; 
     yPossion += aButton.frame.size.width-15; 
     [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width ,yPossion- 
     50)]; 
    } 

    UILabel *label = [[UILabel alloc] init]; 
    [label setTranslatesAutoresizingMaskIntoConstraints:YES]; 

    [label setText:cat.Name]; 
    [label setTextColor:[UIColor blackColor]]; 
    label.font = [UIFont systemFontOfSize:12]; 

    [label sizeToFit]; 

    [label setFrame:CGRectMake(4, 44, 70, 60)]; 
    [scrollView addSubview:label]; 
    [aButton addSubview:label]; 

    } 
    } 

ответ

0
UIButton *test = [self buttonWithTitle:@"test" target:self selector:@selector(testDoit) frame:CGRectMake(self.view.center.x-50, 270.0, 100.0, 50.0) : 12]; 


- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame :(int)fontSize{ 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button setFrame:frame]; 
     [button setTitle:title forState:UIControlStateNormal]; 
     button.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize]; 
     button.userInteractionEnabled = YES; 
     button.titleLabel.textAlignment = UITextAlignmentCenter; 
     button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation; 
     button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0); 
     button.titleLabel.textColor = [UIColor blackColor]; 
     button.showsTouchWhenHighlighted = YES; 
     button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleRightMargin| 
     UIViewAutoresizingFlexibleTopMargin| 
     UIViewAutoresizingFlexibleHeight| 
     UIViewAutoresizingFlexibleBottomMargin; 
     [button addTarget:self action:inSelector forControlEvents:UIControlEventTouchUpInside]; 
     //[button autorelease]; 
     return button; 
    } 
+0

это для нескольких кнопок, но как его переход из следующего Полноэкранный, когда я нажимаю на кнопки –

+0

с помощью '@selector (testDoit)'. добавьте движущийся экранный код init –

+0

'[button addTarget: self action: inSelector forControlEvents: UIControlEventTouchUpInside];' –

Смежные вопросы