0

UIButton 在 @implementation 下创建。我必须在某些功能后将其删除,但[button removeFromSuperview]不起作用。我怎样才能删除它?

#import "TabBar.h"
#import "CustomCell.h"
@interface TabBar ()

@end

@implementation TabBar
UILabel *title;
NSString *par;
UIButton *button;

NSMutableArray *backlist;

- (NSString *)documentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                NSUserDomainMask, YES) lastObject];
}
-(void)tbl_info{
    NSString *path = [[self documentsDirectory] stringByAppendingPathComponent:@"icd10.plist"];

    self.dataList = [[NSMutableArray alloc] initWithContentsOfFile:path];    
    for (NSInteger i=[self.dataList count]-1; i>=0;i--) {
        NSDictionary *d = [self.dataList objectAtIndex:i];
        par=[d objectForKey:@"PARENTID"];
        if(![par isEqualToString:self.val])
            [self.dataList removeObjectAtIndex:i];


    }
}

- (IBAction)ara:(id)sender{
    [self tbl_info];
    [self.tViewTab reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [self.dataList count];
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name= [[self.dataList objectAtIndex:indexPath.row] objectForKey:@"NAME"];
    CGSize maxSize = CGSizeMake(320/2, MAXFLOAT);
    CGSize labelSize = [name sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
    if(labelSize.height>=63)
        labelSize.height=63;
    return labelSize.height+1;
}

- (UITableViewCell*)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    static NSString *cellID = @"cellid";

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = (CustomCell*)[[[NSBundle mainBundle]
                              loadNibNamed:@"CustomCell" owner:nil options:nil]
                             lastObject];
    }

    NSDictionary *d = [self.dataList objectAtIndex:indexPath.row];
    cell.nameLabel.text = [d objectForKey:@"NAME"];
    cell.cityLabel.text = [d objectForKey:@"CODE"];
    cell.indexPath = indexPath;
    CGSize maxSize = CGSizeMake(320/2, MAXFLOAT);
    CGSize nameSize = [cell.nameLabel.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
    if(nameSize.height>=63)
        nameSize.height=63;
    cell.nameLabel.frame =CGRectMake(80, 0, 234, nameSize.height);
    cell.cityLabel.text = [d objectForKey:@"CODE"];
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *d = [self.dataList objectAtIndex:indexPath.row];
    NSMutableDictionary *backprop = [[NSMutableDictionary alloc] init];
    [backprop setObject:self.val forKey:@"id"];
    [backprop setObject:title.text forKey:@"name"];

    [backlist addObject:backprop];

    //back button
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(touchEvent:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:title.text forState:UIControlStateNormal];
    button.frame = CGRectMake(02.0, 02.0, 71, 32);
    [self.view addSubview:button];

    //parent and title label
    [title setText:[d objectForKey:@"NAME"]];
    self.val=[d objectForKey:@"ID"];    

    [self tbl_info];
    [self.tViewTab reloadData];

}
-(void)touchEvent:(id)sender{
    [title setText:[[backlist objectAtIndex:backlist.count-1] objectForKey:@"name" ]];
    self.val= [[backlist objectAtIndex:backlist.count-1] objectForKey:@"id"];
    [backlist removeObjectAtIndex:backlist.count-1];
    if(backlist.count>0)
        [button setTitle:[[backlist objectAtIndex:backlist.count-1] objectForKey:@"name" ] forState:UIControlStateNormal];
    else
        [button removeFromSuperview];

    [self tbl_info];
    [self.tViewTab reloadData];
}
- (void)viewDidLoad
{
    [super viewDidLoad];

        backlist =[[NSMutableArray alloc]init];

    self.val=@"0";
    [self tbl_info];
    [self.tViewTab reloadData];

    title =[[UILabel alloc]initWithFrame:CGRectMake(79.0, 07.0, 162, 21)];
    title.text = @"ICD10 Kod Grupları"; //etc...
    [self.view addSubview:title];
    [title release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
4

2 回答 2

1

代替这个:

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(touchEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:title.text forState:UIControlStateNormal];
button.frame = CGRectMake(02.0, 02.0, 71, 32);
[self.view addSubview:button];

尝试这个 :

 if (button != nil) {
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(touchEvent:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:title.text forState:UIControlStateNormal];
    button.frame = CGRectMake(02.0, 02.0, 71, 32);
    [self.view addSubview:button];
 }

或者试试这个:

if (button != nil) {
    [button removeFromSuperView];
    [button release]; // dont write this in case of ARC
    button = nil;
 }
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(touchEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:title.text forState:UIControlStateNormal];
button.frame = CGRectMake(02.0, 02.0, 71, 32);
[self.view addSubview:button];
于 2013-08-15T14:08:45.977 回答
0

不是解决方案,而是解决方法:

利用

[sender removeFromSuperview];

或分别,因为 sender 是 typeid而不是UIView*

[(UIButton*)sender removeFromSuperview];

但是您可以将发件人更改为 UIView*。-(void)touchEvent:(UIView *)sender{

有的甚至放在UIButton*那里。我个人不喜欢这样,但是如果您确定该消息UIButtons仅由发送者发送,那将非常节省。

另一种解决方法是设置 . 按钮的属性,然后通过其标签tag获取真正的按钮。self.view

于 2013-08-15T14:08:17.593 回答