Question

I have below code to create simple NSButton in a separate function

-(void)myFunction
{
    NSButton *btn = [self createButton:@"Button_Name"];

    if(some condition )
    {
      [btn setEditable:YES];
    }
}

- (NSButton*)createButton:(NSString *)buttonName 
{
    NSButton *btn = [[NSButton alloc] initWithFrame:NSMakeRect(20, 0, 20, 20)];
    [btn setButtonType:NSSwitchButton];
    [btn setImagePosition:NSImageOnly];
    [btn setTarget:self];
    [btn setTitle: buttonName];

    return btn;
}

In my same It is working fine.I am using this code in a Big project.Will It work normally or will cause some problem.Is this a correct way?

Was it helpful?

Solution

Few things I would like to bring in your notice:

  1. You pass buttonName and buttonTitle but never uses it.

  2. You create an object of type NSButton but your object name is against the convention, by reading btnCell someone will expect it to be NSButtonCell.

  3. In the above code I cant see any reference to the newly created button and even you are not adding it to any view. (I hope in your real Big project you are not missing those.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top