0
그래서 iOS 개발에 익숙하지 않고 다른 클래스에 버튼 클릭 이벤트를 위임하려고합니다. 경고의 버튼을 클릭 할 때마다 앱이 다운되고 Thread_1 EXC_BAD_ACCESS 오류가 발생합니다.UIAlertView를 다른 클래스/파일로 위임 하시겠습니까? 작동 안함?
이것은 내 코드입니다.
// theDelegateTester.h
#import <UIKit/UIKit.h>
@interface theDelegateTester : UIResponder <UIAlertViewDelegate>
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
구현 ..
// theDelegateTester.m
#import "theDelegateTester.h"
@implementation theDelegateTester
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"Delegated");
}
@end
그리고 여기, 당신은 항상 대문자로 클래스 이름을 시작해야, 모든
#import "appleTutorialViewController.h"
#import "theDelegateTester.h"
@interface appleTutorialViewController()
- (IBAction)tapReceived:(id)sender;
@end
@implementation appleTutorialViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)tapReceived:(id)sender {
theDelegateTester *newTester = [[theDelegateTester alloc] init];
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"This is a delegated alert" delegate:newTester cancelButtonTitle:@"Close" otherButtonTitles:@"Cool!", nil];
[myAlert show];
}
@end
흠, 그래 그게 할 것 같다,하지만 왜 난 그냥 tapReceived 내에서 객체를 생성하고 위임 매개 변수로에 통과 할 수 없다는 것입니다? 나는 그 물건을 물건에 전해야합니까? 또한 del 또는 myAlert에 대한 릴리스를 호출 할 때도 프로젝트에서 ARC (Automatic Reference Counting)를 사용하고 릴리스하지 않습니다. 알다시피, ARC는 기본적으로 자동 가비지 수집이며 더 이상 객체를 사용하지 않을 때 객체를 릴리스합니다. –
속성을 만들지 않으면 TheDelegateTester의 인스턴스가 범위를 벗어나서 메모리에서 제거됩니다. 이 속성을 속성으로 만들면 해당 속성에서 메모리에서 제거되지 않는지 확인합니다. 귀하의 두 번째 질문에 : 예. 완전히 동일하지는 않지만 ARC를 유지/해제 할 필요는 없습니다. –
문제는 위임자가 약한 (비 보유 된) 참조라는 점입니다. 즉, 뷰 컨트롤러가 TheDelegateTester 인스턴스에 대한 참조를 유지할 책임이 있음을 의미합니다. 그리고 그것은 당신이하지 않는 것입니다. – DrummerB