2011-01-25 168 views
0

在iphone sdk中如何從另一個類調用一個類?從另一個類調用一個類

我需要訪問此background.m類,

-(void)brightness 
{ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    UIImage *image = [UIImage imageNamed:@"brightness.jpg"]; 
    button.frame = CGRectMake(0, 0, image.size.width, image.size.height); 
    [button setImage:image forState:UIControlStateNormal]; 

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    UIImage *image1 = [UIImage imageNamed:@"brightness.jpg"]; 
    button1.frame = CGRectMake(0, 0, image1.size.width, image1.size.height); 
    [button1 setImage:image forState:UIControlStateNormal]; 
    [button1 addTarget:self action:@selector(brightnessControl:) forControlEvents:UIControlEventTouchUpInside]; 

    gBrightnessSetting=100; 
    brightnessOverlay = [[CALayer alloc] retain]; 
    brightnessOverlay.masksToBounds = YES; 
    brightnessOverlay.backgroundColor = [[[UIColor blackColor] colorWithAlphaComponent:1.0] CGColor]; 
    brightnessOverlay.opacity = 0.0; 
    [self.layer addSublayer:brightnessOverlay]; 

    bottomButtonsSize = SCREENWIDTH/5; 

} 
- (void)dealloc { 
    [brightnessLessButton release]; 
    [brightnessMoreButton release]; 

    [super dealloc]; 
} 

- (void) setLayerFrames { 
    brightnessOverlay.frame = CGRectMake(self.layer.bounds.origin.x,self.layer.bounds.origin.y,self.bounds.size.width,self.layer.bounds.size.height); 
} 

In ebook.m class, 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if ([indexPath row]==0) { 
     background *back=[[background alloc] init]; 
     [back brightness]; 

    } 
+0

由於對象方法調用對象方法,因此您的問題很難回答。你能提供一些代碼示例來展示你的意思嗎? – 2011-01-25 03:44:52

+0

我需要訪問這個背景類, – user579911 2011-01-25 03:46:30

回答

1

在Objective-C,指一經一個實例調用與-開始,而當一個類被調用的方法與+開始方法。

例如,如果你想調用構造方法,如+ (NSArray *)arrayWithArray:(NSArray *)array中的NSArray:

NSArray *firstArray = [[NSArray alloc] init]; 
NSArray *duplicateArray = [NSArray arrayWithArray:firstArray]; 
[duplicateArray retain]; 

你可能也注意到alloc方法開始於+,因爲它是在一個類中調用,而不是一個實例。