2011-12-21 34 views
-1

我有setter方法。我從我的第一堂課設置了價值,並且我能夠在第二堂課中獲得我的價值。但是當我把這個雙重值調用到另一個顯示零的方法時。爲什麼會發生這種情況?
這是我的代碼。 在我能夠來到這裏的價值二傳手:我怎麼能指定雙重獲得從setter方法iphone

-(void)setbasic:(double)basic1{ 
    basic = basic1; 
    NSLog(@"setbasic: %f",basic); 
    //console output 
    //setbasic: 104234.000000 
} 

設定值:

-(void)baseValue{ 
    Advanced *advace=[[Advanced alloc]init]; 
    double myDouble=([textfield0.text doubleValue]); 
    [advace setbasic:myDouble]; 
} 

之後我打電話,在我的方法,像這樣同一類的另一種方法基本雙重價值:

-(void)calculate{ 
    //here is the problem i can't get basic value 
    NSLog(@"calculatesetbasic: %f",basic); 
    //console output 
    //basic: 0.0000 
} 

這裏我只獲得0
我叫兩個方法是這樣的:

-(IBAction)Pressed: (id) sender{ 
    Advanced *advace=[[Advanced alloc]init];    
    [advace baseValue]; 
    [advace calculate]; 
} 
+0

什麼方法家住哪一類裏面? – 2011-12-21 09:07:15

+0

@Mats Stijlaart - (void)計算方法我需要在這裏獲得setter值。 – 2011-12-21 09:09:10

+0

@MatsStijlaart - (void)計算方法是同一個類。我在哪裏分配設置值。 – 2011-12-21 09:11:31

回答

1

我怎麼看它:

你的流程是這樣的:

->Pressed gets called. 
    Makes an advanced. 
    Calls baseValue in advanced. 
    ->baseValue gets called. 
     baseValue creates a new advanced 
     retrieves the double. 
     sets the double in the new advance. 
    Calls calculate in advanced. 

我覺得你的問題是在創造新的先進。你創建它兩次。首先在你的控制器中。然後再次在您的高級方法(baseValue)中。

相反的:

baseValue creates a new advanced 
retrieves the double. 
sets the double in the new advance. 

做:

retrieves the double. 
sets the double in the **same** advance. ([self setbasic:value]) 
+0

我能夠檢索雙值我無法將該值加載到另一種方法。 – 2011-12-21 09:39:24

+0

是的,但是您的應用程序是否在沒有創建第二個高級對象的情況下工作?並且請做一些關於你的英語,這真的很糟糕。 '我可以檢索double值並將該值傳遞給另一個方法'。 – 2011-12-21 09:42:30

+0

是的你的權利,我的英語很poor.i發展感謝您的答覆。 – 2011-12-21 10:18:20

相關問題