开发者

My NSMutable Array isn't adding a button

I'm trying to add button to a scroll view using a NSMutableArray. The code works fine in a twitter example I am using as a reference, but for some reason it is not working in this case. In the twitter search case the code is implemented in the main class. While in this case I am implementing it in the flipside view. Could anyone help me out since I am a bit new to Objective c. Thanks in Advance.

Here is the init where the NSMutableArray Buttons is initialized

  - (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

Here we have the method where we are adding the object to the buttons array but it stays empty.

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

Here is the dealloc where I release the buttons array.

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

I can't Figure out where the mistake is.

Here is the Code that is supposed to add a new button.

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

NSString *key = ItemTitleField.text; // get the text in t开发者_JS百科agField
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:


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜