[英]How do I access instance variables inside an instance method called from an instance method at the same level
我有一個連接到滑塊的IBAction實例方法,並在名稱為datacellR1的文本字段中顯示滑塊值。 下面是該代碼的副本,后面是一個問題。 這兩種方法都位於View2Controller.m文件的@implementation部分中。
- (IBAction)slider1Change:(id)sender
{
float pctVal1 = [slider1 floatValue]; // this works
[datacellR1 setFloatValue:pctVal1];
[View2Controller CalculateUpdatedTotal ]; // This method needs to work with the datacellR1 contents, but I can’t access it.
}
-(void)CalculateUpdatedTotal
{
// -------- do some work with datacellR1 ----
// This function fails with an error
float newValue = [datacellR1 floatValue];
//some other code goes here
}
slide1Change中的錯誤是未找到CalculateUpdatedTotal方法。 如果將CalculateUpdatedTotal從實例方法更改為類方法,則錯誤是在類方法中訪問了實例變量datacellR1。
關於如何進行這項工作有什么建議嗎?
如所寫, CalculateUpdatedTotal
也是一個實例方法。 因此,要調用它,您應該將消息傳遞給self
,而不是類( View2Controller
):
[self CalculateUpdatedTotal];
順便說一句,通常在Objective-C中以小寫字母開頭的方法名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.