Objective c 将视图添加到子视图会使我的应用程序崩溃

Objective c 将视图添加到子视图会使我的应用程序崩溃,objective-c,uiview,crash,subview,Objective C,Uiview,Crash,Subview,当我添加子视图时,它会使我的应用程序崩溃。发生的情况如下: View加载调用了另一个类中的方法,该类然后调用原始类中的sortDataIntoSubs。然后尝试添加一个子视图,但它崩溃了,因为我认为它循环了代码,这导致了对象超出数组边界的问题。我该怎么办 - (void)viewDidLoad { searchText = [searchText uppercaseString]; self.title = searchText; Download_Data *dwnLD = [[Downlo

当我添加子视图时,它会使我的应用程序崩溃。发生的情况如下: View加载调用了另一个类中的方法,该类然后调用原始类中的sortDataIntoSubs。然后尝试添加一个子视图,但它崩溃了,因为我认为它循环了代码,这导致了对象超出数组边界的问题。我该怎么办

 - (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

}

视图由导航控制器处理。

您正在初始化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];

谢谢,但不用了,还是会崩溃。