如何使用不同的NSArrays加载相同的tableview取决于轻敲的uibuttons?

时间:2013-02-16 11:24:24

标签: uitableview ios5 uibutton tableview subview

在我的应用程序中,我有一个滚动视图和一个表 - 视图添加为子视图,在这个滚动视图上我有多个菜单按钮(UI按钮)作为子视图添加到我的滚动视图。我想要根据相应的菜单按钮加载表 - 具有不同数组的视图被点击。任何想法?

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self PutButtoninScrollView:7];

}
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
    myButton1=[[UIButton alloc]init];
    myButton1= [UIButton buttonWithType:UIButtonTypeCustom];
    myButton1.frame=CGRectMake(5, 5, 70, 30);
    [myButton1 setTitle:@"दुनिया" forState:UIControlStateNormal];
    [myButton1 setTag:1];
    myButton1.backgroundColor=[UIColor blackColor];
    [myButton1 addTarget:self action:@selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myScrollView addSubview:myButton1];

    myButton2=[[UIButton alloc]init];
    myButton2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton2.frame=CGRectMake(80, 5, 70, 30);
    [myButton2 setTitle:@"India" forState:UIControlStateNormal];
    [myButton2 addTarget:self action:@selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myScrollView addSubview:myButton2];
    [self.myScrollView setContentSize:CGSizeMake(160, 35)];
    _myScrollView.backgroundColor=[UIColor redColor];
    [self.view addSubview:self.myScrollView];
}

-(void)ListViewAction:(id)sender
{
    NSLog(@"sucessful");        

    if ([myButton.currentTitle isEqualToString:@"दुनिया"])
    {
        NSLog(@"successful again 1");
    }
    else if ([myButton.currentTitle isEqualToString:@"भारत"])
    {
        NSLog(@"successful again 2");
    }        
}

问题是当我执行代码时 显示“成功”。这意味着调用ListViewAction方法,但我的if语句永远不会被执行。

3 个答案:

答案 0 :(得分:0)

试试这个

    -(void)PutButtoninScrollView:(int)numberOfbuttons
{
xPosition=5;
for(int i=0;i<numberOfbuttons;i++)
{

    myButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame=CGRectMake(xPosition, 5, 70, 30);
    myButton.backgroundColor=[UIColor redColor];
   [myButton setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
    [myButton setTag:i];
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.myScrollView addSubview:myButton];
    xPosition+=75;
}
[self.myScrollView setContentSize:CGSizeMake(xPosition,35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}

  - (void) buttonClicked:(id) sender
{
   NSString *strTitle = [sender currentTitle];
 // arrItem have list of array items
   NextView  *detailViewController = [[NextView alloc] initWithNibName:@"NextView" bundle:nil];
   detailViewController.arrTableView =[arrItem objectAtIndex:[strTitle integerValue]];
  [self.navigationController pushViewController:detailViewController animated:YES];


}

答案 1 :(得分:0)

- (无效)ListViewAction:(ID)发送方 {     的NSLog(@ “SUCESSFUL”);

if ([[sender currentTitle] isEqualToString:@"दुनिया"])
{
    NSLog(@"successful again 1");
}
else if ([[sender currentTitle] isEqualToString:@"भारत"])
{
    NSLog(@"successful again 2");
}        

}

答案 2 :(得分:-1)

试试这个

[myButton1 addTarget:self action:@selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[myButton2 addTarget:self action:@selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];