내 앱의 간단한 구조는 다음과 같습니다.다른 뷰 컨트롤러 내부에서 uiviewcontroller의 액션 호출하기
루트 뷰 컨트롤러 인 ViewConroller1이 있습니다.
ViewController1에는 다른 View Controller 인 ViewController2의 인스턴스가 10 개있는 scrollview가 있습니다.
ViewController2에는 xib가 있고 그 xib에는 UIButton이 있습니다. UIButton은 ViewController2 .m 파일에 액션을 가지고 있습니다 - 탭 할 때마다 발생하는 것으로 간주됩니다.
앱을 실행할 때 버튼을 터치하면 예외가 발생하여 종료됩니다. ViewController2를 루트보기 컨트롤러로 만들면 모든 것이 정상적으로 실행됩니다.
ViewConroller1을 루트보기 컨트롤러로 올바르게 실행하려면 어떻게해야합니까?
일부 코드 :ViewController1 (GSMainVC.m)
#import "GSMainVC.h"
#import "GSViewController.h"
@interface GSMainVC()
@end
@implementation GSMainVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
int n=10;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width*n, self.view.frame.size.height);
for (int i = 0; i <n; i++){
GSViewController *gsVC = [[GSViewController alloc] init];
CGRect cellFrame = CGRectMake(gsVC.view.frame.size.width*i, 0, gsVC.view.frame.size.width, gsVC.view.frame.size.height);
gsVC.view.frame = cellFrame;
[scrollView addSubview:gsVC.view];
}
[self.view addSubview:scrollView];
}
@end
ViewController2 .H (GSViewController.h)
#import <UIKit/UIKit.h>
@interface GSViewController : UIViewController
@property (nonatomic) NSInteger number;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *subtitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *daysPlayedLabel;
@property (weak, nonatomic) IBOutlet UIButton *repeatButton;
- (IBAction)toggleRepeat:(id)sender;
@end
ViewConroller2하는 .m (GSViewController.m)
#import "GSViewController.h"
@implementation GSViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (IBAction)toggleRepeat:(id)sender {
NSLog (@"hello");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
@end
어떤 예외가 있습니까? – RaffAl
이 10 개의 인스턴스를 하위 뷰 컨트롤러로 추가 하시겠습니까? 그렇지 않다면,해야합니다. 더 일반적인 질문은 왜 10 개의 뷰를 사용하는 것과는 대조적으로 10 개의 뷰 컨트롤러가 필요한가요? – rdelmar
참조 http://raywenderlich.com/10209/my-app-crashed-now-what-part-1 – rmaddy