【问题标题】:My NSMutable Array isn't adding a button我的 NSMutableArray 没有添加按钮
【发布时间】:2011-06-07 17:08:36
【问题描述】:

我正在尝试使用 NSMutableArray 将按钮添加到滚动视图。该代码在我用作参考的 twitter 示例中运行良好,但由于某种原因,它在这种情况下不起作用。在 twitter 搜索案例中,代码在主类中实现。在这种情况下,我在反面视图中实现它。任何人都可以帮助我,因为我对Objective c有点陌生。 提前致谢。

这里是初始化 NSMutableArray 按钮的 init

  - (id)init
    {
self = [super init]; // initialize the superclass members

if (self != nil) // if the superclass initialized properly
{      
    // creates list of valid directories for saving a file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                                         NSDocumentDirectory, NSUserDomainMask, YES);

    // get the first directory
    NSString *dir = [paths objectAtIndex:0];

    // concatenate the file name "tagsIndex.plist" to the path
    filePath = [[NSString alloc] initWithString:
                [dir stringByAppendingPathComponent:@"tagsIndex.plist"]];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    // if the file does not exist, create an empty NSMutableDictionary;
    // otherwise, initialize an NSDictionary with the file's contents 
    if ([fileManager fileExistsAtPath:filePath] == NO)
    {
        tags = [[NSMutableDictionary alloc] init];
    } // end if
    else
    {
        tags = [[NSMutableDictionary alloc]
                initWithContentsOfFile:filePath];
    } // end else


    buttons = [[NSMutableArray alloc] init]; // create array

    infoButtons = [[NSMutableArray alloc] init]; // create array
} 
return self;  // if self == nil, object not initialized properly
} // end method init

这里我们有将对象添加到按钮数组但它保持为空的方法。

- (void)addNewButtonWithTitle:(NSString *)title
 {
// create a new button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

// give the button the title of the tag
[button setTitle:title forState:UIControlStateNormal];

// tell the button to call buttonTouched: when it is touched
[button addTarget:self action:@selector(buttonTouched:)
 forControlEvents:UIControlEventTouchUpInside];

[buttons addObject:button]; // add the UIButton to the end of the array
                               //This Doesn't add it stays 0;   
// sort the NSMutableArray by the UIButton's titles
[buttons sortUsingSelector:@selector(compareButtonTitles:)];

[self refreshList]; // refresh the list of favorite search Buttons

// Adjust the content size of the view to include the new button. The
// view scrolls only when the content size is greater than its frame.
CGSize contentSize = CGSizeMake(scrollView.frame.size.width,
                                buttons.count * (BUTTON_HEIGHT + BUTTON_SPACING) + BUTTON_SPACING);
[scrollView setContentSize:contentSize];
}

这是我释放按钮数组的dealloc。

 - (void)dealloc {
[buttons release];
[super dealloc];
}

我不知道错误在哪里。

这是应该添加新按钮的代码。

   - (IBAction)addTag:sender
  {
// make the keyboard disappear
[ItemTitleField resignFirstResponder];
[QuantityField resignFirstResponder];

NSString *key = ItemTitleField.text; // get the text in tagField
NSString *value = QuantityField.text; // get the text in queryField

// test if either field is empty
if (value.length == 0 || key.length == 0)
    return; // exit from the method

if ([tags valueForKey:key] == nil) // test if the tag already exists
    [self addNewButtonWithTitle:key]; // if not, add a new button

[tags setValue:value forKey:key]; // add a new entry in tags

ItemTitleField.text = nil; // clear tagField of text
QuantityField.text = nil; // clear queryField of text

[tags writeToFile:filePath atomically:NO]; //save the data
 } // end method addTag:

【问题讨论】:

  • 你确定,当你打电话给[buttons addObject:button];buttons 不是零?你怎么知道这不加按钮?
  • 在您的代码中,我看不到您将按钮添加到视图的位置。你能提供这个吗?
  • 我正在调试代码,它告诉我按钮有 0 个对象
  • count 发送到nil 对象将返回0。您能否验证它不是nil

标签: objective-c


【解决方案1】:
- (void)addNewButtonWithTitle:(NSString *)title
{
        // create a new button
        //  UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    UIButton *button = [[UIButton alloc] init];


    or


    UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

        // give the button the title of the tag
    [button setTitle:title forState:UIControlStateNormal];

        // tell the button to call buttonTouched: when it is touched
    [button addTarget:self action:@selector(buttonTouched:)
     forControlEvents:UIControlEventTouchUpInside];

    [buttons addObject:button]; // add the UIButton to the end of the array
                                //This Doesn't add it stays 0;   
                                // sort the NSMutableArray by the UIButton's titles
    [buttons sortUsingSelector:@selector(compareButtonTitles:)];

    [self refreshList]; // refresh the list of favorite search Buttons

        // Adjust the content size of the view to include the new button. The
        // view scrolls only when the content size is greater than its frame.
    CGSize contentSize = CGSizeMake(scrollView.frame.size.width,
                                    buttons.count * (BUTTON_HEIGHT + BUTTON_SPACING) + BUTTON_SPACING);
    [scrollView setContentSize:contentSize];
}

【讨论】:

  • 使用此滚动视图将subviewtofront 并验证您已将子视图添加到滚动视图
  • 尝试了bringsubviewtoFront,当我尝试添加新标签时它只是崩溃并退出应用程序。我在界面生成器的滚动视图中手动添加了一个圆角矩形。一旦我按下执行 addTag 操作的按钮,按钮就会消失。它与 Delegate 有关系吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
相关资源
最近更新 更多