当我点击主视图上的标签时,我遇到了这个错误,该标签指向详细信息视图(应该显示在ReasonLibrary的数据模型中显示的标签。
我的代码:
主视图控制器标题
@interface MasterViewController : UIViewController
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *reasonLabelViews;
@end
主视图控制器实现
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "Reasons.h"
@interface MasterViewController ()
@end
@implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
for (NSUInteger index = 0; index < self.reasonLabelViews.count; index++) {
Reasons *reason = [[Reasons alloc] initWithIndex:index];
UILabel *reasonLabelView = self.reasonLabelViews[index];
reasonLabelView.text = reason.reasonsRazao;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UILabel *reasonLabelView = (UILabel *)[sender view];
if ([self.reasonLabelViews containsObject:reasonLabelView]) {
NSUInteger index = [self.reasonLabelViews indexOfObject:reasonLabelView];
DetailViewController *detailViewController = (DetailViewController *)segue.destinationViewController;
detailViewController.reason = [[Reasons alloc] initWithIndex:index];
}
}
- (IBAction)showReasonDetail:(id)sender {
[self performSegueWithIdentifier:@"showReasonDetail" sender:sender];
}
@end
详细信息视图控制器标题
#import <UIKit/UIKit.h>
@class Reasons;
@interface DetailViewController : UIViewController
@property (strong, nonatomic) Reasons *reason;
@property (weak, nonatomic) IBOutlet UILabel *reasonLabel;
@property (weak, nonatomic) IBOutlet UILabel *motiveLabel;
@property (weak, nonatomic) IBOutlet UILabel *zeroFiveLabel;
@end
详情视图控制器实施
#import "DetailViewController.h"
#import "Reasons.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (self.reason) {
self.reasonLabel.text = self.reason.reasonsRazao;
self.motiveLabel.text = self.reason.randomFact;
self.zeroFiveLabel.text = self.reason.reasonsDeZeroACinco;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ReasonsLibrary标题
#import <Foundation/Foundation.h>
extern NSString *const kRazao;
extern NSString *const kMotivo;
extern NSString *const kDeZeroACinco;
@interface ReasonLibrary : NSObject
@property (strong,nonatomic) NSArray *library;
- (NSString *)randomFact;
@end
ReasonsLibrary实施
#import "ReasonLibrary.h"
@implementation ReasonLibrary
NSString *const kRazao = @"razao";
NSString *const kMotivo = @"motivo";
NSString *const kDeZeroACinco = @"dezeroacinco";
- (instancetype)init
{
self = [super init];
if (self) {
_library = @[@{kRazao: @"Razao 1",
kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
kDeZeroACinco: @"1",
},
@{kRazao: @"Razao 2",
kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
kDeZeroACinco: @"2",
},
@{kRazao: @"Razao 3",
kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
kDeZeroACinco: @"3",
},
@{kRazao: @"Razao 4",
kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
kDeZeroACinco: @"4",
},
@{kRazao: @"Razao 5",
kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
kDeZeroACinco: @"5",
},
@{kRazao: @"Razao 6",
kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"],
kDeZeroACinco: @"6",
}
];
}
return self;
}
- (NSString *)randomFact {
int random = arc4random_uniform((int)self.library.count);
return [self.library objectAtIndex:random];
} @end
原因标题
#import <Foundation/Foundation.h>
@interface Reasons : NSObject
@property (strong, nonatomic) NSString *reasonsRazao;
@property (strong, nonatomic) NSString *reasonsMotivo;
@property (strong, nonatomic) NSString *reasonsDeZeroACinco;
@property (strong, nonatomic) NSString *randomFact;
-(instancetype)initWithIndex:(NSUInteger)index;
@end
实施原因
#import "Reasons.h"
#import "ReasonLibrary.h"
@implementation Reasons
-(instancetype)initWithIndex:(NSUInteger)index {
self = [super init];
if (self) {
ReasonLibrary *reasonlibrary = [[ReasonLibrary alloc] init];
NSArray *library = reasonlibrary.library;
NSDictionary *reasonsDictionary = library[index];
_reasonsRazao = [reasonsDictionary objectForKey:kRazao];
_reasonsMotivo = [reasonsDictionary objectForKey:kMotivo];
_reasonsDeZeroACinco = [ reasonsDictionary objectForKey:kDeZeroACinco];
}
return self;
}
@end
答案 0 :(得分:2)
密钥kMotivo
的对象在您的代码中为NSArray
。您将其分配给NSString
属性,然后分配给UILabel文本属性。 UILabel
尝试在NSArray
上调用长度并因异常而崩溃。
以下内容将起作用:
NSArray *motives = [reasonsDictionary objectForKey:kMotivo];
if (motives != nil && motives.count > 0) {
int random = arc4random_uniform(motives.count);
_reasonsMotivo = motives[random];
}
上面的代码将从数组中获取第一个可用动机,并将其分配给_reasonsMotivo
。它应该插入而不是代码行:
_reasonsMotivo = [reasonsDictionary objectForKey:kMotivo];
答案 1 :(得分:0)
而不是:
kMotivo: @[@"Por que sim",@"Por que demais",@"Por que muito",@"Por que foda",@"Por que top",@"Por que demasiado"]
使用
kMotivo: [[NSArray alloc]initWithObjects:@"Chapter 1",@"Chapter 2",@"Chapter 3",@"Chapter 4",@"Chapter 5",nil]