我在UISegmentControl上添加了自定义徽章。在Xcode 4.0.2 SDK 4.3
它的工作正常。但是在Xcode 4.2 SDK 5.0
中,当选择其他项目时,不会显示自定义徽章。我不知道为什么会这样?我正在使用IOS 4.3 Xcode 4.0.2
的以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
super.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wallpaper.png"]];
NSLog(@"Root View Loaded");
segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
@"Home",@"Surveys",@"Results",@"Create",@"Settings",
nil]];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 310, 40);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor darkGrayColor];
segmentedControl.momentary = NO;
segmentedControl.highlighted = YES;
segmentedControl.selectedSegmentIndex = 0;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
NSArray *segmentBarArray = [NSArray arrayWithObjects:
segmentBarItem,nil];
[[self appDelegate] setSegmentedControl:segmentedControl];
[[self appDelegate] setSegmentBarArray:segmentBarArray];
[self setToolbarItems:[[self appDelegate] segmentBarArray] animated:NO];
[[self navigationController] setToolbarHidden:NO animated:YES];
[self.navigationItem setHidesBackButton:YES animated:YES];
self.navigationController.toolbar.tintColor = [UIColor blackColor];
int surveycount = [[self appDelegate] getUnreadSurveyCount];
surveyCountBadge = [CustomBadge customBadgeWithString:[NSString stringWithFormat:@"%d",surveycount]];
[surveyCountBadge setFrame:CGRectMake(105, -10, surveyCountBadge.frame.size.width, surveyCountBadge.frame.size.height)];
[segmentedControl addSubview:surveyCountBadge];
if (surveycount == 0) {
[surveyCountBadge setHidden:YES];
}else{
[surveyCountBadge setHidden:NO];
}
}
-(void)segmentAction:(id) sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(@"selected index is %d",segmentedControl.selectedSegmentIndex);
if (segmentedControl.selectedSegmentIndex == 0) {
[self.navigationController popToRootViewControllerAnimated:YES];
} else if(segmentedControl.selectedSegmentIndex ==1){
surveyForMeViewController = [[SurveyForMeView alloc] initWithNibName:@"SurveyForMeView" bundle:nil];
[self.navigationController pushViewController:surveyForMeViewController animated:YES];
} else if(segmentedControl.selectedSegmentIndex ==2){
resultViewController = [[ResultView alloc] initWithNibName:@"ResultView" bundle:nil];
[self.navigationController pushViewController:resultViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==3) {
newSurveyViewController = [[NewSurveyView alloc] initWithNibName:@"NewSurveyView" bundle:nil];
[self.navigationController pushViewController:newSurveyViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==4) {
settingsViewControlle = [[SettingsView alloc] initWithNibName:@"SettingsView" bundle:nil];
[self.navigationController pushViewController:settingsViewControlle animated:YES];
}
}
我应该在此提及其他任何事情吗?
答案 0 :(得分:2)
您正在为segmentedControl添加子视图。由于您不知道系统在选择其他项目时如何重绘控件,您无法说明您的视图是否会被删除。让control,button或segmentedControl完成其工作,如果要添加徽章,请将其添加为其父级的子视图。
[surveyCountBadge setFrame:CGRectMake(105.0f+segmentedControl.frame.origin.x,
-10.0f+sementedControl.frame.origin.y, surveyCountBadge.frame.size.width,
surveyCountBadge.frame.size.height)];
[[segmentedControl superView] addSubview: surveyCountBadge];