UISegmentedcontrol 与 UINavigationcontrol - UISC 消失
我有一个带有 UISegmentedcontrol 的 UINavigationcontrol 和两个选项。这两个选项推送到不同的 UIViewcontroller。当用户按下第二个选项时,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;
但没有结果。
当我转到 UIViewController 时,当我按下第二段时,UISegmentControl 会显示,但当我返回到第一段时,UISegmentControl 就会消失。
有人吗?
此致, xqtr
好吧,当我尝试使用它时,segmentedcontrol 从一开始就消失了。我使用:
Step3.h:
#import <UIKit/UIKit.h>
@interface Step3 : UIViewController {
UISegmentedControl * segmentedControl;
}
@property (nonatomic, retain) IBOutlet UISegmentedControl * segmentedControl;
@end
Step3.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)。
有什么建议吗? :)
I have a UINavigationcontrol with UISegmentedcontrol and two options. The two options pushes to different UIViewcontrollers. When the user push the second option the UISegmentControl is still there, but when the user push the first option again the UISegmentControl disappears. What code do I need there?
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];
}
I've tried:
Step3 *step3 = [[[Step3 alloc] initWithNibName:@"Step3"
step3.navigationItem.titleView = segCtl;
but with no results.
The UISegmentControl shows when I go to the UIViewController, when I press the second segment, but disappears when I go back to the first segment.
Anyone?
Best regards,
xqtr
Okey, when I try to use it, the segmentedcontrol disappears from the beginning. I use:
Step3.h:
#import <UIKit/UIKit.h>
@interface Step3 : UIViewController {
UISegmentedControl * segmentedControl;
}
@property (nonatomic, retain) IBOutlet UISegmentedControl * segmentedControl;
@end
Step3.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
I use exactly the same code in step3.h/m and step6.h/m, but now when I tried your snippet the Segmentedcontrol disappears already in the start view (step3).
Any suggestions? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是有关如何更新导航栏的文档;
http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006934-CH3-SW25
一般情况我不会将视图控制器的 navigationItem 中应该包含的内容具体化。相反,让堆栈中的每个视图控制器实现 viewWillAppear 以将正确的 titleView 放入其自己的 navigationItem 中。例如,在上面
Step3.m 引用的 Step3 类中...
这样做可以消除 CoreDataMenuAppDelegate 必须了解所有视图控制器的内部详细信息的责任。
另外,查看 WWDC 2010 上的模型-视图-控制器演讲可能会有所帮助;
http://developer.apple.com/videos/wwdc/2010/
演讲是 iPhone OS 的会话 116 模型-视图-控制器。
第 116 节充满了有关如何思考视图控制器以及类之间的功能划分的信息。具体来说,演示者讨论了尊重控制器类之间的封装的重要性。
Here are the docs on how updating the nav bar happens;
http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006934-CH3-SW25
In general I'd not externalize what should be in the navigationItem for a view controller. Instead have each view controller in your stack implement viewWillAppear to place the correct titleView into it's own navigationItem. For example in the Step3 class you reference above
Step3.m...
Doing this removes the responsibility from CoreDataMenuAppDelegate from having to know the internal details of all your view controllers.
Also it might help to view the Model-View-Controller talk from WWDC 2010;
http://developer.apple.com/videos/wwdc/2010/
The talk is Session 116 Model-View-Controller for iPhone OS.
Session 116 is full of information on how to think about view controllers and the devision of functionality between classes. Specifically the presenter discusses how important it is to respect encapsulation between your controller classes.