如何根据点击的uibuttons加载具有不同NSArrays的相同tableview?
Posted
技术标签:
【中文标题】如何根据点击的uibuttons加载具有不同NSArrays的相同tableview?【英文标题】:how to load same tableview with different NSArrays depending on uibuttons tapped? 【发布时间】:2013-02-16 11:24:23 【问题描述】:在我的应用程序中,我添加了一个滚动视图和一个表格视图作为子视图,在这个滚动视图上,我将多个菜单按钮(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 语句从未执行。
【问题讨论】:
我们可以借助tag或者id指针来实现... 【参考方案1】:试试这个
-(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];
【讨论】:
可能是 uibutton 添加为子视图的问题。我已经阅读了有关 UIGestureRecognizer 的信息。但是不知道怎么用【参考方案2】:-(void)ListViewAction:(id)sender NSLog(@"成功");
if ([[sender currentTitle] isEqualToString:@"दुनिया"])
NSLog(@"successful again 1");
else if ([[sender currentTitle] isEqualToString:@"भारत"])
NSLog(@"successful again 2");
【讨论】:
最终它以这种方式工作。只需将发件人而不是我的按钮放在 ListViewActionthanx 为您的帮助@Ravindhiran【参考方案3】:试试这个
[myButton1 addTarget:self action:@selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[myButton2 addTarget:self action:@selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
【讨论】:
对不起,我的代码中是 (ListViewAction:)。我的 ListViewAction 工作正常。我得到输出字符串“成功”。我想要的是执行“再次成功1”和“再次成功2” 终于找到了解决办法:-(void)ListViewAction:(id)sender if ([[sender currentTitle] isEqualToString:@"दुनिया"]) NSLog(@"successful again"); else if ([[sender currentTitle] isEqualToString:@"भारत"]) NSLog(@"successful again1");以上是关于如何根据点击的uibuttons加载具有不同NSArrays的相同tableview?的主要内容,如果未能解决你的问题,请参考以下文章
Swift - 如何使 UIButton 仅在附加的图像上点击交互并忽略透明部分
iOS - 如何正确缩放具有不同图像大小的 UIButton(如 Facebook 新闻提要)?