简体   繁体   English

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

[英]programmatically created button click to open a tableview or popup

I have created a button programmatically in a UITableViewCell and I want it to open a new table view or popup when clicked. 我已经在UITableViewCell中以编程方式创建了一个按钮,并且希望它在单击时打开新的表格视图或弹出窗口。 Please give me some tutorial or hint. 请给我一些教程或提示。 Below is my code: 下面是我的代码:

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
}

If you just want to show a popup when tapping the button, you can use UIAlertVIew 如果您只想在点击按钮时显示弹出窗口,则可以使用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];
}

Here a sample to do it 这是一个样本来做

//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;
}

Click Management 点击管理

-(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];
}

I assume you are using a UINavigationController with your UITableView . 我假设您将UINavigationControllerUITableView

If so, you can do try this to make it open a new table view: 如果是这样,您可以尝试这样做以使其打开新的表格视图:

  • Make a new class which is a subclass of UITableViewController . 制作一个新类,它是UITableViewController的子类。 Make sure the checkbox named "With XIB for user interface" is checked. 确保选中名为“使用XIB作为用户界面”的复选框。 Give it a name, ie "MyTableViewController". 给它起一个名字,即“ MyTableViewController”。
  • Open the implementation file (the one ending in .m) of your class where you have your dropDownClick method, and import your new class with #import "MyTableViewController.h" . 打开具有dropDownClick方法的类的实现文件(以.m结尾的文件),然后使用#import "MyTableViewController.h"导入新类。 Do this at the top of your class (.m file). 在班级顶部(.m文件)执行此操作。
  • Edit your dropDownClick method to the following lines: 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.
}

You could also do this without using a .xib, but then it would be a bit different. 您也可以在不使用.xib的情况下执行此操作,但这会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM