3

I have a IBAction method which is called when a button touched. But when the UIViewController is called, that IBAction method is called first before viewdidload. I checked my code, there is nowhere that I call IBAction method specificly.

What is the reason that method called before viewdidload? Why does it happen?

Thank you. .h

 @interface VC_PatientInfo : M_SwipeInterface

.m

  @implementation VC_PatientInfo
    static M_PatientRow* patient;
    NSMutableArray*activeDrugList;
    NSInteger orderId;
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    +(NSInteger)getOrderId{
        return orderId;
    }
    +(void)setOrderId:(NSInteger)oId{
        orderId=oId;
    }
    - (void)viewDidLoad
    {
        orderId=0;
[super viewDidLoad];
        patient= [VC_PatientList getPatient];
        activeDrugList = [[NSMutableArray alloc]init];
        activeDrugList=[DAO_GetPatientActiveDrugs drug:[VC_Login getGuid] visit_id:[VC_PatientList getPatient].visit_id];
        if([VC_Login is_from_monitoring]){
            [DAO_InsertMonitoriingList patient:[VC_Login getGuid] pId:[self.pId_lbl.text integerValue]];
            [self.txt_monitoring setText:@"Takip Listemden Çıkar"];
            self.txt_monitoring.textColor= [UIColor redColor];

        }
        // Do any additional setup after loading the view.
    }

    +(NSMutableArray*)refreshActiveDrugs{

        activeDrugList=[DAO_GetPatientActiveDrugs drug:[VC_Login getGuid] visit_id:[VC_PatientList getPatient].visit_id];
        return activeDrugList;
    }
    +(NSMutableArray*)getActiveDrugList{
        return activeDrugList;
    }
    - (IBAction)call_DrugOrder:(id)sender {
        [Global setVC:@"vc_drugorder"];
        [self callVC];
    }
    - (IBAction)setMonitoringList:(id)sender {
        if([self.txt_monitoring.text isEqualToString:@"text1"]){
            [DAO_InsertMonitoriingList patient:[VC_Login getGuid] pId:[self.pId_lbl.text integerValue]];
            [self.txt_monitoring setText:@"text2"];
            self.txt_monitoring.textColor= [UIColor redColor];
        }else{
            [DAO_RemoveMonitoriingList patient:[VC_Login getGuid] pId:[self.pId_lbl.text integerValue]];
            [self.txt_monitoring setText:@"text1"];
            self.txt_monitoring.textColor= [Global colorWithHexString:@"99CC00"];
        }
    }


    - (IBAction)setViewSecret:(id)sender {
        if(self.viewSecret.frame.size.height==143 )
           [self.viewSecret setFrame:CGRectMake(self.viewSecret.frame.origin.x,
                                               self.viewSecret.frame.origin.y,
                                               self.viewSecret.frame.size.width,
                                               0)];
        else
            [self.viewSecret setFrame:CGRectMake(self.viewSecret.frame.origin.x,
                                                 self.viewSecret.frame.origin.y,
                                                 self.viewSecret.frame.size.width,
                                                 143)];

    }
    - (IBAction)goBack:(id)sender {
        [self dismissViewControllerAnimated:NO completion:nil];
    }
    - (IBAction)callAllergyVC:(id)sender {
        [Global setVC:@"vc_patientallergy"];
        [self callVC];
    }
    - (IBAction)callDiagnosisVC:(id)sender {
        [Global setVC:@"vc_patientdiagnosis"];
        [self callVC];
    }
    - (IBAction)callVCOldOrder:(id)sender {
        [Global setVC:@"vc_oldorder"];
        [self callVC];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end
user3309441
  • 107
  • 11

1 Answers1

4

There are some things you can check, trying to solve this:

Callers

Go to the implementation of the method, and stand with the caret on on of the lines of code inside it. No on the top left corner of the main window click the small button with the black and gray rectangles (To the left of the arrows) and check if there is any place in your caode where someone is calling this method.

You won't see outlets here - so if the Callers option is gray (Like in the picture below) - You are good.

callers

Methods call stack

Try putting a breakpoint on the first line of the method implementation. When the breakpoint stops you code, look at the left in the Navigator pane, under the Debugger, you can see the exact order in which the methods called eachother to get to the call of your method. Most chances you will only need to just go one step back (Just click the line to see where it happens - btw this is kind of like a time-machine (or scope-machine?) where you can see what was the state of all the objects in your scope).

call stack

Property Outlets

Go the where the outlet is defined (in your .h or .m file) in click that tiny gray circle. This will show you all the outlet connections to that method/property (In the following picture I clicked a property but the beginning of method will be the same).

Property Outlets

NIB/Storyboard connections

Open you NIB/Storyboard file, and click the View Controller itself (The top item in the Document Outline chain on the left, another options is Ctrl+Shift+Mouse Click the view controller in the IB and choose the View Controller from the drop menu).

Storyboard

Now on the right (In the Utilities pane) go to the Connection Inspector (The most right one) to see all the connections to everything in the View Controller. Check you method to see if anything is connected that shouldn't be.

enter image description here

Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
  • I created my IBAction from Stroyboard with draging button Touchup inside into .m file. So there is no defination in .h file and it created with (id)sender. So I created IBAction in .h file, then I drag the method in storyboard file to the UIButton and it worked. But I still don't understand why (id)sender make it called before viewdidload.Do you know the reason of it? – user3309441 Apr 30 '14 at 06:40
  • Great answer missing only one thing - put a breakpoint in the method and look at the call stack. – jrturton Apr 30 '14 at 06:59
  • Basically speaking, all the outlets from you storyboard don't even exist in `viewDidLoad` (trying to call them will give you nil), and only in `viewWillAppear` you actually have the outlets, so I'm assuming the problem lies somewhere else... – Aviel Gross Apr 30 '14 at 07:36
  • 1
    @jrturton indeed I went pretty far and forgot about the most reasonable option of all... I edited my answer... – Aviel Gross Apr 30 '14 at 07:47
  • Unfortunately I can't upvote it twice :) this is really good work, well presented and a great debugging checklist – jrturton Apr 30 '14 at 08:31
  • So what caused the problem OP had? – Blaszard Mar 17 '16 at 13:39
  • I tried all the solutions mentioned above. Everything seems right. But still IBAction is called before viewDidLoad – anamika41 Jan 19 '17 at 06:39