break; from an action

zots

Re-member
is there a way to break out of an action. for example when a button is clicked in my cocoa app, it calls an action with some if statements that check the values in textfields. if the values are invalid an alert comes up and then i want it to skip the rest of the code until the textfield is valid. the rest of the code should only be run if the data in the textfields passes the if statements.

if you dont understand what i'm trying to explain, it is the equivalent of exit sub in VB.
 
Code:
-(IBAction)checkData:(NSButton*)sender
{
    NSString* ref;
 
    if([self isValid:(ref=[textField1 stringValue])]) [self randomFunct:ref];
    if([self isValid:(ref=[textField2 stringValue])]) [self randomFunct:ref];
    if([self isValid:(ref=[textField3 stringValue])]) [self randomFunct:ref];
    ...
}

-(BOOL)isValid:(NSString*)text
{
   //chec if it's valid
}

-(void)randomFunct:(NSString*)text
{
   //process data
}

HTH,
F-bacher
 
i think what he actually meant was whether it was possible to completely break out of the function. suppose you had a lot of code behind your three if's.

sure it is possible. suppose, your method has a void return, you'd just write

Code:
if (!necessaryConsition) return;

whatever comes behind that would not be executed.
note: IBAction is the same as void from a programmatic point of view. IBAction only is a hint to IB when parsing header files to actually display those actions.

if your method has another return value but void, return nil and make sure the caller checks the return value and doesn't do whatever action it would usually do, using nil as a pointer is not really a good idea...

Code:
id result = [self myMethodThatMightReturnNil];
if (result)
{
	do something with the result here
}

hope that helps.

edit: think before you type... ;)
 
Back
Top