2014-12-04 6 views
0

다음 코드를 가지고 있는데, 나는 내가 얻고있는 로그를 이해하지 못하기 때문에 함께 작성했습니다. 원래 버튼 중 하나에 연결된 액션이있는 곳의 원래 파일이있었습니다. 코드는 Im이 시도하는 것처럼 뷰를 변경하지 않으므로 대신 태그로 버튼을 연결하고 탭 이벤트에 대한 메소드를 추가하려고했습니다. 이제 두 메소드가 동일하다는 점을 유의하십시오. 차이점은 두 메소드가 연결되는 방법뿐입니다 (두 메소드 모두 호출됩니다). 그러나 IBAction 메서드에서 sunPushed currentVC는 NULL입니다 (로그는 아래에 게시되지만 지점을 얻습니다). 누군가가 주제에 대해 어떤 생각을 밝힐 수 있습니까? 하루 종일 저를 괴롭혀서 왜 이런 일이 일어나고 있는지 알 수 없습니다. 어떤 도움이라도 대단히 감사합니다!보기 컨트롤러에서 호출 된 사용자 정의 클래스의 IBAction과 관련된 큰 문제

이 headerfile (. 나는 프로그래머 컴파일러가 등록 방법 대 IBAction를 처리하는 방법에 약간의 차이를 생각해 봤는데) :

#import <UIKit/UIKit.h> 

@interface FooterViewTest1 : UIView 
//@property (strong, nonatomic) UIButton *button1; 
@property (strong, nonatomic) UIViewController *currentVC; 
@property (strong, nonatomic) IBOutlet UIButton *button1; 

-(void)buttonPushed; 
-(void)initButtons:(UIViewController *)viewController; 
@end 

구현

#import "FooterViewTest1.h" 
#import "StartViewController.h" 

@implementation FooterViewTest1 

-(void)initButtons:(UIViewController *)viewController{ 

/*UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 180)]; 
[button1 setImage:[UIImage imageNamed:@"menu_bar_sun_black"] forState:UIControlStateNormal]; 
[button1 addTarget:self action:@selector(buttonPushed) forControlEvents:UIControlEventTouchUpInside]; 
[viewController.view addSubview:button1];*/ 
self.currentVC = viewController; 

NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"RaymioFooterView" owner:nil options:nil]; 
UIView *footerView = [viewController.view viewWithTag:1]; 
UIView *footerViewContent = [nibContents lastObject]; 
NSLog(@"SELFinside initbuttons%@", self); 
NSLog(@"currentVCinside initbuttons%@", self.currentVC); 

UIButton *button1X = (UIButton *)[footerViewContent viewWithTag:5]; 
[button1X addTarget:self action:@selector(buttonPushed) forControlEvents:UIControlEventTouchUpInside]; 

[footerView addSubview:footerViewContent]; 
} 

- (IBAction)sunPushed{ 
NSLog(@"SELFinside sunpushed%@", self); 
NSLog(@"currentVCinside sunpushed%@", self.currentVC); 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
StartViewController *startViewController = [storyboard instantiateViewControllerWithIdentifier:@"StartViewController"]; 
[self.currentVC presentViewController:startViewController animated:YES completion:nil]; 
} 

-(void)buttonPushed{ 
NSLog(@"SELFinside buttonpushed%@", self); 
NSLog(@"currentVCinside buttonpushed%@", self.currentVC); 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
StartViewController *startViewController = [storyboard instantiateViewControllerWithIdentifier:@"StartViewController"]; 
[self.currentVC presentViewController:startViewController animated:YES completion:nil]; 
} 

@end 

은 아래 이미지를 참조

enter image description here

다시 한번 감사드립니다!

초기화하는 footerView :

self.footerViewTest = [FooterViewTest1 new]; 
//self.footerViewTest.currentVC = self; 
[self.footerViewTest initButtons:self]; 

답변

0

난 당신이 무슨 일을하는지 잘 모르겠어요 -하지만 당신의 로그 self 두 개의 서로 다른 개체입니다 : 주소 0X에서 1 개체를 ...으로 6f0 buttonPushedinitButtons 내부 다른 주소 0x30 ... b90 안에 sunPushed. 전체 수업을 보여줄 수 있습니까?

편집 :

귀하의 문제가 IBAction를 스토리 보드를 통해 설정되고 있으며, 뷰가 new의 호출을 통해서 생성되는 것입니다. 스토리 보드에 정의 된대로 액션을 사용하는 대신 :

self.footerViewTest = [FooterViewTest1 new]; 

스토리 보드의보기에 속성을 첨부하십시오. 그렇게하면 코드에서 동일한 (단일!) 인스턴스를 갖게됩니다.

+0

네, 당신이 말하는 것을 봅니다. 그것이 내 전체 FooterView 클래스입니다. 내 VC (들)에서 할 일은 내 속성 footerView에서 new를 호출 한 다음 해당 속성에서 initButtons :를 호출하는 것입니다. 따라서 의도적으로 IBAction을 복사 한 이후로 IBAction이 제대로 작동하지 않는 것에 대한 좌절감은 새로운 방법으로 넣은 다음 버튼에 터치 이벤트를 수동으로 추가하여 미쳐 버리지 않도록합니다. 무언가가 진행되고있는 것처럼 보입니다. – DevilInDisguise

+0

그리고 currentVC는 스토리 보드에 연결된 IBOutlet입니까? – KerrM

+0

FooterView 클래스의 속성은 없습니다. CurrentVC는 footerView 클래스를 초기화하고 initButtons : self를 호출하는 View Controller로 설정됩니다. – DevilInDisguise