2011-06-07 47 views
0

我想使用NSMutableArray將按鈕添加到滾動視圖。該代碼在我用作參考的twitter示例中工作正常,但出於某種原因,它在這種情況下不起作用。在twitter搜索的情況下,代碼在主類中實現。雖然在這種情況下,我正在實現它在flipside視圖。任何人都可以幫助我,因爲我對Objective c有點新。 在此先感謝。我NSMutable陣列沒有添加一個按鈕

這裏就是按鈕的NSMutableArray初始化

- (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: 
+0

你肯定的是,當你調用'[按鈕ADDOBJECT:按鈕]','buttons'不是零?你怎麼知道這不會添加按鈕? – Saphrosit 2011-06-07 17:14:45

+0

嘗試記錄'按鈕'。 – 2011-06-07 17:27:46

+1

在您的代碼中,我看不到您將按鈕添加到視圖的位置。你能提供這個嗎? – sergio 2011-06-07 17:38:18

回答

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

回答代碼可能來自:http://pastie.org/1654643/wrap – 2011-06-07 18:13:00

+0

任何方式是否有用這個工作 – 2011-06-07 18:15:03

+0

我試過了,但仍然沒有按鈕顯示在滾動視圖中.. – 2011-06-07 18:18:27