我的應用程序具有簡單的結構:調用另一個的ViewController內側UIViewController中的動作
有一個ViewConroller1其是根視圖控制器;
ViewController1有一個帶有另一個視圖控制器ViewController2的10個實例的滾動視圖。
在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