从另一个类调用一个类

问题描述:

在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

在Objective-C,指一经一个实例调用与-开始,而当一个类被调用的方法与+开始方法。

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

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

你可能也注意到alloc方法开始于+,因为它是在一个类中调用,而不是一个实例。