2011-05-30 64 views
0

我有一個viewcontroller(mainViewController)和視圖類(UIView類,mainView)。 mainViewController,我從mainView類調用了一個方法「generateView」。下面是代碼:UIButton未能調用函數?

方法1:

- (void) generateView { 
    container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)]; 
    container.backgroundColor = [UIColor clearColor]; 
    container.contentSize = CGSizeMake(500,1200); 
    container.showsVerticalScrollIndicator = YES; 
    [container setScrollEnabled:TRUE]; 
    container.backgroundColor = [UIColor redColor]; 
    int row = 0; 
    int col = 0; 
    for (int i = 0; i < 5; i++) { 
     if(i % 4 == 0 && i != 0){ 
      row++; 
      col = 0; 
     } 
     UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)]; 
     NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@/images/items/%i.png", HOSTADDR, i ]]]; 
     [b setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal]; 
     [b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]]; 
     [b setTag: i]; 
     [b addTarget:self action:@selector(testFunction:) forControlEvents:UIControlEventTouchUpInside]; 
     [b setEnabled:TRUE]; 
     [b setUserInteractionEnabled:TRUE]; 
     [container addSubview:b]; 
     col++; 
    } 
    [self addSubview:container]; 
} 


- (void) testFunction: (id) sender { 
    NSLog(@"Function called. Sender tag number: %i", [sender tag]); 
} 

方法2:

- (void) generateView { 
    container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)]; 
    container.backgroundColor = [UIColor clearColor]; 
    container.contentSize = CGSizeMake(500,1200); 
    container.showsVerticalScrollIndicator = YES; 
    [container setScrollEnabled:TRUE]; 
    container.backgroundColor = [UIColor redColor]; 
    int row = 0; 
    int col = 0; 
    for (int i = 0; i < 5; i++) { 
     if(i % 4 == 0 && i != 0){ 
      row++; 
      col = 0; 
     } 
     UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)]; 
     NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@/images/items/%i.png", HOSTADDR, i ]]]; 
     [b setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal]; 
     [b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]]; 
     [b setTag: i]; 
     [b addTarget:mainViewController action:@selector(updateData:) forControlEvents:UIControlEventTouchUpInside]; 
     [b setEnabled:TRUE]; 
     [b setUserInteractionEnabled:TRUE]; 
     [container addSubview:b]; 
     col++; 
    } 
    [self addSubview:container]; 
} 

我真正需要的是方法2,因爲我想打電話給在mainViewController的updateData,但無論我如何嘗試,按鈕不是調用應該被調用的函數。任何想法?

編輯:

這是我怎麼叫視圖類

- (void) loadContent: (id) sender { 
    [self removeSubmenus]; 
    switch ([sender tag]) { 
     case 2001: 
     { 
      MainView *mainView = [[MainView alloc] initWithSubmenu:CGRectMake(420, 85, 0, 0)]; 
      [self.view addSubview:mainView]; 

      break; 
    } 
    default: 
     break; 
} 

}

回答

0

您應該設置delaysContentTouchesYESUIScrollView

你也在泄漏一堆記憶。確保您在完成後立即釋放UIButtonb)和NSDataimageData)對象。

- 編輯:我試過你的代碼,它工作正常。但是你似乎想要一個UIView中的所有代碼,所以我把一個快速示例放在一起。

TestView.h

#import <UIKit/UIKit.h> 

@interface TestView : UIView {} 

@end 

TestView.m

#import "TestView.h" 

@implementation TestView 

- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame]; 
    if (self) { 

     UIScrollView *container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)]; 
     int row = 0; 
     int col = 0; 
     for (int i = 0; i < 5; i++) { 
      if(i % 4 == 0 && i != 0){ 
       row++; 
       col = 0; 
      } 
      UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)]; 

      [b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]]; 
      [b setTag: i]; 
      [b addTarget:self action:@selector(testFunction:) forControlEvents:UIControlEventTouchUpInside]; 
      [container addSubview:b]; 
      [b release]; 
      col++; 
     } 
     [self addSubview:container]; 

    } 
    return self; 
} 

- (void) testFunction: (id) sender { 
    NSLog(@"Function called. Sender tag number: %i", [sender tag]); 
} 

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

@end 

TestController.h

@interface TestController : UIViewController { } 
@end 

TestController.m:

#import "TestController.h" 
#import "TestView.h" 
@implementation TestController 

- (void) generateView { 
    TestView *tv = [[TestView alloc]initWithFrame:[self.view frame]]; 
    [self.view addSubview:tv]; 
    [tv release]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self generateView]; 
} 

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

@end 
+0

注意到釋放的對象。但我嘗試將delayedContentTouches設置爲yes,但仍然沒有運氣先生 – iDaniel 2011-05-30 05:11:43

+0

我在容器外創建了另一個UIButton(uiscrollview),但它仍然未能調用該函數,你知道爲什麼嗎? (很奇怪..) – iDaniel 2011-05-30 05:16:26

+0

嘗試在滾動視圖中將setUserInteractionEnabled設置爲YES。 – csano 2011-05-30 05:17:34

0

嘗試將此代碼添加到您的方法

container.delaysContentTouches = NO; 
container.canCancelContentTouches = NO; 

請修改代碼以避免內存泄漏。確保刪除您擁有所有權的對象。

+0

感謝您指出這一點,我現在釋放這些對象。我跟着你和CS的方法,但仍然沒有運氣:( – iDaniel 2011-05-30 05:13:01

+0

你能看到我的第二個評論爲@cs? – iDaniel 2011-05-30 05:16:48

0

試試這個,

[b addTarget:mainViewController.view action:@selector(updateData:) forControlEvents:UIControlEventTouchUpInside]; 
+0

沒有運氣先生這是我的updateData函數在mainViewController中: - (void)updateData:(id)sender { NSLog(@「Sender:%i」,[sender tag]); } – iDaniel 2011-05-30 05:08:31

+0

嘗試通過爲mainviewController創建對象 或在主視圖中創建一個委託來調用mainviewcontroller updateData。 – Navaneethan 2011-05-30 05:28:10

+0

如果我在mainViewController中創建按鈕,但我想分開函數我想要使用UIView類生成視圖,而不是在viewController類 – iDaniel 2011-05-30 05:32:39