gpt4 book ai didi

iphone - 当UISegmentedcontrol项更改时,为什么它会隐藏IOS 5中的自定义徽章?

转载 作者:行者123 更新时间:2023-12-03 19:57:02 25 4
gpt4 key购买 nike

我在 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];
}


}

这里我还应该提到什么吗?

最佳答案

您正在将 subview 添加到segmentedControl。由于您不知道选择另一个项目时系统如何重绘控件,因此您无法确定您的 View 是否会被删除。让控件、按钮或segmentedControl 完成其工作,如果您想添加徽章,请将其添加为其父 View 的 subview 。

[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];

关于iphone - 当UISegmentedcontrol项更改时,为什么它会隐藏IOS 5中的自定义徽章?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com