<分区>
<分区>
当我在通向详细 View 的主视图中单击标签时遇到了这个错误(它应该显示在 ReasonLibrary 的数据模型中显示的标签。
我的代码:
主视图 Controller 标题
@interface MasterViewController : UIViewController
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *reasonLabelViews;
@end
主视图 Controller 实现
#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
详细 View Controller 标题
#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
详细 View Controller 实现
#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];
} @结束
原因标题
#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
最佳答案
键kMotivo
的对象是您代码中的NSArray
。您将其分配给 NSString
属性,然后分配给 UILabel 文本属性。 UILabel
尝试在 NSArray
上调用 length 并因异常而崩溃。
像下面这样的东西会起作用:
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];
关于ios - [__NSArrayI 长度] : unrecognized selector sent to instance when moving through views,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31174077/
相关文章:
objective-c - 与 DCRoundSwitch 一起使用时 UIView 动画不起作用
objective-c - 核心动画 : Frame Rate
ios - 将密码和 session token 保存在钥匙串(keychain)中
swift - 从模态视图转到选项卡栏 View Controller 并且不会丢失选项卡栏
ios - 在不同的 Storyboard、Swift、iOS、Xcode 上通过 segue 和 Navigation 将数据从一个 Controller 传递到另一个 Controller
ios - 更改 UIVisualEffectView 的效果
ios - 如何使用 Swift 从 Parse 中的 _User 类获取 currentUserInfo
iphone - UINavigationbar 覆盖 View 顶部