我有一个带有 UISegmentedcontrol 和两个选项的 UINavigationcontrol。这两个选项推送到不同的 UIViewcontrollers。当用户按下第二个选项时 UISegmentControl 仍然存在,但是当用户再次按下第一个选项时 UISegmentControl 消失。我需要什么代码?
CoreDataMenuAppDelegate.h:
#import <UIKit/UIKit.h>
@interface CoreDataMenuAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UINavigationController *navigationController;
UINavigationController *navigationController2;
UITabBarController *tabBarController;
IBOutlet UISegmentedControl *myMent;
}
-(IBAction)segmentAction:(id)sender;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController2;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
CoreDataMenuAppDelegate.m:
#import "CoreDataMenuAppDelegate.h"
#import "RootViewController.h"
#import "Step3.h"
#import "Step6.h"
@implementation CoreDataMenuAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize navigationController2;
@synthesize tabBarController;
-(void)viewDidLoad
{
[myMent addTarget:self action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
myMent.selectedSegmentIndex = 0 ;
}
- (IBAction) segmentAction:(id)sender
UISegmentedControl* segCtl = sender ;
if( [segCtl selectedSegmentIndex] == 0 )
{
[navigationController2 popToRootViewControllerAnimated:NO];
//What to put here?
}
if( [segCtl selectedSegmentIndex] == 1 )
{
NSLog(@"hi this is second segment");
Step6 *step6 = [[[Step6 alloc] initWithNibName:@"Step6" bundle:nil] autorelease];
[self.navigationController2 pushViewController:step6 animated:NO];
step6.navigationItem.titleView = segCtl;
}
}
- (void)dealloc {
[navigationController release];
[navigationController2 release];
[tabBarController release];
[window release];
[super dealloc];
}
我试过了:
Step3 *step3 = [[[Step3 alloc] initWithNibName:@"Step3"
step3.navigationItem.titleView = segCtl;
但没有结果。
UISegmentControl 在我转到 UIViewController 时显示,当我按下第二段时,但当我回到第一段时消失。
任何人?
最好的问候, xqtr
好的,当我尝试使用它时,segmentedcontrol 从一开始就消失了。我用:
Step3.h:
#import <UIKit/UIKit.h>
@interface Step3 : UIViewController {
UISegmentedControl * segmentedControl;
}
@property (nonatomic, retain) IBOutlet UISegmentedControl * segmentedControl;
@end
步骤3.m:
#import "Step3.h"
@implementation Step3
@synthesize segmentedControl;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationItem.titleView = self.segmentedControl;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
@end
我在 step3.h/m 和 step6.h/m 中使用完全相同的代码,但是现在当我尝试您的代码段时,Segmentedcontrol 已经在开始视图中消失(步骤 3)。
有什么建议么?:)