以编程方式创建按钮,单击打开表视图或弹出窗口



我在UITableViewCell中以编程方式创建了一个按钮,我希望它在单击时打开一个新的表视图或弹出窗口。请给我一些指导或提示。下面是我的代码:

UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0,0,360,25);
[button addTarget:self action:@selector(dropDownClick) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)dropDownClick
{
    //Here I know there should be some code but I am not getting what it should be since I am new to iPad/iPhone development
}

如果你只是想在点击按钮时显示一个弹出窗口,你可以使用UIAlertVIew

- (IBAction)dropDownClick {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title of popup" 
                                                    message:@"Did this popup show?" 
                                                   delegate:self 
                                          cancelButtonTitle:@"Yes" 
                                          otherButtonTitles:nil];
    [alert addButtonWithTitle:@"No"];
    [alert show];
}

下面是一个示例

//DISEGNO DELLE CELLE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier;
CellIdentifier = @"CellStorico";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    CGRect tRect1 = CGRectMake(0.0f, 3.0f, 768.0f, 40.0f);
    id title1 = [[UIButton alloc] initWithFrame:tRect3];
    [title1 addTarget:self action:@selector(ClickCheck:) forControlEvents:UIControlEventTouchUpInside];
    [title1 setTag:3];
    [cell addSubview:title1];
    [title1 release];
}


if ([[[Storico objectAtIndex:[indexPath row]] objectForKey:@"Selezionato"] isEqualToString:@"0"]) {
    [(UIButton *) [cell viewWithTag:3] setImage:[UIImage imageNamed:@"NoCheck.png"] forState:UIControlStateNormal];
    [(UIButton *) [cell viewWithTag:3] setImage:[UIImage imageNamed:@"NoCheck.png"] forState:UIControlStateHighlighted];
    [(UIButton *) [cell viewWithTag:3] setImage:[UIImage imageNamed:@"NoCheck.png"] forState:UIControlStateSelected];        
} else {
    [(UIButton *) [cell viewWithTag:3] setImage:[UIImage imageNamed:@"Check.png"] forState:UIControlStateNormal];
    [(UIButton *) [cell viewWithTag:3] setImage:[UIImage imageNamed:@"Check.png"] forState:UIControlStateHighlighted];
    [(UIButton *) [cell viewWithTag:3] setImage:[UIImage imageNamed:@"Check.png"] forState:UIControlStateSelected];
}   

return cell;
}

单击管理

-(void)ClickCheck:(id)sender{
//Ex. Load a New View
FrmScadenzeGenerali *SchermataScadenzeGenerali=[[FrmScadenzeGenerali alloc] initWithNibName:@"FrmScadenzeGenerali" bundle:nil];
[[self navigationController] pushViewController:SchermataScadenzeGenerali animated:YES];
[SchermataScadenzeGenerali release];

//Ex. PopUp a View
[self.view insertSubview:[TabTestata view] atIndex:0];
//Show a Message
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"Attenzione!" message:@"Error Text Message To Show!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [Alert show];
    [Alert release];
}

我假设您使用的是UINavigationControllerUITableView

如果是,你可以试着这样做,使它打开一个新的表视图:

  • 新建一个UITableViewController的子类。确保选中名为"With XIB for user interface"的复选框。给它起个名字,比如。"MyTableViewController"。
  • 打开包含dropDownClick方法的类的实现文件(以.m结尾的文件),并使用#import "MyTableViewController.h"导入新类。在班上名列前茅的时候做这件事。m文件)。编辑您的dropDownClick方法到以下行:

-(IBAction)dropDownClick
{
    MyTableViewController *vc = [[MyTableViewController alloc] initWithNibName:@"MyTableViewController"]; // Loads a .xib file called "MyTableViewController.xib"
    [self.navigationController pushViewController:vc animated:YES]; // This is where the swapping happens.
    [vc release]; // Remember to do this if your app does not use ARC.
}

您也可以不使用。xib,但那样会有点不同。

最新更新