objective-c - 将 View 添加到 subview 会使我的应用程序崩溃

标签 objective-c uiview crash subview

当我添加一个 subview 时,它会使我的应用程序崩溃。继承人发生了什么:
View确实加载了另一个类中的方法,然后该类在原始类中调用sortDataIntoSubs。然后尝试添加一个 subview ,但是由于我认为它循环了代码而导致对象超出数组问题,因此它崩溃了。我该怎么办?

 - (void)viewDidLoad
{
searchText = [searchText uppercaseString];
self.title = searchText;
Download_Data *dwnLD = [[Download_Data alloc]init];
NSDate *finishDate = [NSDate date];
NSDate *startDate = [NSDate date];
NSDateComponents *dayComponent = [[[NSDateComponents alloc] init] autorelease];
dayComponent.day = -1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
startDate = [theCalendar dateByAddingComponents:dayComponent toDate:finishDate options:0];
[dwnLD downloadCSVFile:searchText startDate:startDate finishDate:finishDate key:1];
[super viewDidLoad];
}


-(void) sortDataIntoSubs:(NSMutableArray *) arrMTemp
{
NSMutableArray *arrDate = [[NSMutableArray alloc] init];
NSMutableArray *arrOpen = [[NSMutableArray alloc] init];
NSMutableArray *arrHigh = [[NSMutableArray alloc] init];
NSMutableArray *arrLow = [[NSMutableArray alloc] init];
NSMutableArray *arrClose = [[NSMutableArray alloc] init];
NSMutableArray *arrVolume = [[NSMutableArray alloc] init];
NSMutableArray *arrAdjClose = [[NSMutableArray alloc] init];
//Add every 7th object to the arrDate array.

[self setUpView];
}

-(void)setUpView
{

CGRect screenBound = [[UIScreen mainScreen] bounds];
UISegmentedControl *segMeg = [[UISegmentedControl alloc]init];
segMeg.center = CGPointMake(screenBound.size.width / 2,screenBound.size.height / 2);
segMeg.segmentedControlStyle = UISegmentedControlStylePlain;
NSArray *arrSegMeg = [NSArray arrayWithObjects:@"1w",@"2w",@"8w",@"16w",@"25w", nil];
[segMeg initWithItems:arrSegMeg];
[self.view addSubview:segMeg]; //This crashes

}

该 View 由导航 Controller 处理。

最佳答案

您要初始化segMeg两次。尝试像这样重新排列,看看是否有帮助:

CGRect screenBound = [[UIScreen mainScreen] bounds];
NSArray *arrSegMeg = [NSArray arrayWithObjects:@"1w",@"2w",@"8w",@"16w",@"25w", nil];
UISegmentedControl *segMeg = [[UISegmentedControl alloc] initWithItems:arrSegMeg];
segMeg.center = CGPointMake(screenBound.size.width / 2,screenBound.size.height / 2);
segMeg.segmentedControlStyle = UISegmentedControlStylePlain;
[self.view addSubview:segMeg];

关于objective-c - 将 View 添加到 subview 会使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11730576/

相关文章:

ios - 如何设置初始 Storyboard

objective-c - 判断 NSSlider 旋钮在连续模式下为 'let go' 时

ios - 有人可以解释一下 UIView 的奇怪旋转吗?

iphone - 在 layoutSubviews 中调用 setNeedsDisplay?

objective-c - iOS 12 Xcode 10 应用程序 :openURL:options: Crash

macos - 是否存在阻止 JavaFX 在虚拟化 OS X 环境中运行的已知不兼容性?

objective-c - 相同的命令,不同的结果 setFrame : NSView

objective-c - 在 Cocoa 中激活字体时从 NSFontManager 获取更新的 availableFonts

iPhone UIAnimation 使用 block 不工作

java - Android Studio 构建不断崩溃(stableIds.txt 数据无效。(13))