문제점 : 클래스의 변수가 다른 클래스에서 메소드를 호출 할 때 어떻게 든 간과되고 있습니다.인스턴스 메소드가 클래스 인스턴스 변수를 읽을 수 없습니다.
기본적으로 : 클래스 A, 클래스 B 있습니다. 또한 각각의 클래스에서 메서드 A, 메서드 B가 있습니다.
클래스 B 메서드 B에서 메서드를 호출 할 때 NSLog 값을 사용할 수 있지만 클래스 A 내에 포함 된 NSMutableArray에 액세스 할 수 없습니다. 내 문제가 있습니다.
// Class B
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
foodListingObj = [[FoodListing alloc] initWithNibName:@"FoodListing" bundle:nil];
}
return self;
}
- (void)toggleImage //Method B in Class B {
[foodListingObj didToggle:self.indexOfToggledCell];
}
// Method in Class A
- (void)didToggle:(NSIndexPath *)toggledIndexPath{
//[_toggledIndexArray addObject:toggledIndexPath];
[_toggledIndexArray addObject:@"Anything"];
}
// Method in Class A
- (void)checkArray{
// Log the count of the array
// it always says 1 because I intialize the array
// then add an an object like so [_toggledIndexArray [email protected]"hi"];
// in my ViewDidLoad Method. Hence it appears that the array is still around
// (not deallocated), but yet my method cannot seem to touch it...
NSLog(@"%i",[_toggledIndexArray count]);
}
// dealloc for Class A
- (void)dealloc{
// I release the array among other things
[_toggledIndexArray release];
}
는 배열 (_toggledIndexArray)는 헤더에 선언 한 속성입니다, 그리고
_toggledIndexArray = [[NSMutableArray alloc] init];
오류가있는 클래스 A의있는 viewDidLoad에 초기화 방법 A 배열에 영향을하지 않는 것 사실이다 나에게 알려지지 않은 이유 때문에.
나는 테스트를 위해 수정 된 코드로 위의 질문을 편집했다. 그게 당신이 질문에 대답하는 데 도움이됩니까? 지금 막 더 많은 테스트를 수행 한 결과, didToggle 메소드 내에서 어떤 기능도 수행 할 수 없다는 것을 알게되었습니다. 내 toggleImage 클래스 (클래스 B)에서 호출하면 문제가 발생하는 것으로 보입니다. 감사의 말을 많이 전하며 귀하의 신속하고 유용한 답변에 정말 감사드립니다. – Apollo
코드를 업데이트 해 주셔서 감사합니다. 내 대답은 동일하게 유지됩니다 :). 매번 새로운 FoodListing 객체를 인스턴스화하는 이유는 무엇입니까? "class b"는 클래스 A의 "실제"인스턴스를 가리키는 속성 또는 ivar을 가져야합니다. – jrturton
아, 지금 당신이 말하는 것을 이해합니다. 클래스 B 내의 속성을 사용하여 클래스의 인스턴스를 어떻게 가리 킵니까? 다시 한번 귀하의 도움에 감사드립니다! – Apollo