我希望在主窗口上显示按下信息按钮时创建的modalViewController视图。但是当我按下主窗口上的“信息”按钮时,没有任何反应。
在mainviewcontroller.h文件中,我有以下代码:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface imageviewViewController : UIViewController{
AVAudioPlayer *audioPlayer;
}
@property (nonatomic, retain) UIToolbar *toolbar;
@property (nonatomic, assign) UIBarButtonSystemItem currentSystemItem;
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
@end
在mainviewcontroller.m文件中有以下代码:
#import "imageviewViewController.h"
#import "Infoviewcontroller.h"
@implementation imageviewViewController
@synthesize toolbar;
@synthesize currentSystemItem;
@synthesize audioPlayer;
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:@"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:@selector(Infobuttonpressed)];
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
}
- (void) Infobuttonpressed: (id) sender
{
Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
[self presentModalViewController:myView animated:YES]; // present view modally
[self.navigationController pushViewController:myView animated:YES]; // add to navigation stack
[myView release];
}
在Infoviewcontroller.h文件中有以下代码:
#import <UIKit/UIKit.h>
@interface Infoviewcontroller : UIViewController <UITextViewDelegate>
{
UITextView *textView;
}
@property (nonatomic, retain) UITextView *textView;
@property (nonatomic, assign) UINavigationBar *navBar;
@end
然后在infoviewcontroller.m文件中输入以下代码:
#import "Infoviewcontroller.h"
@implementation Infoviewcontroller
@synthesize textView;
@synthesize navBar;
-(void)dealloc
{
[textView release];
[navBar release];
[super dealloc];
}
-(void)setupTextView
{
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor redColor];
self.textView.font = [UIFont fontWithName:@"System Bold" size:13];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = @"This is UITextView\nThis is UITextView\nThis is UITextView\nThis is UITextView";
[self.view addSubview: self.textView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
navBar = [[UINavigationBar alloc] init];
UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:@"ModalViewControllerTest"] autorelease];
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView:)] autorelease];
navItem.rightBarButtonItem = done;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
答案 0 :(得分:1)
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:@"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:@selector(Infobuttonpressed)];
应该是
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:@"Info"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(Infobuttonpressed:)];
发现差异?首先,您错过了一个目标(应该是self
,而不是nil
)。其次,选择器末尾的冒号是签名的一部分,你错过了它,所以它没有调用你的方法(InfoButtonPressed:(id)sender
)。
顺便说一下,方法应该以小写名称开头。
答案 1 :(得分:0)
你不需要pushModalViewController和pushViewController
假设您的视图已经在UINavigationController的实例中被控制,pushModalViewController本身就可以工作。
编辑 - 删除对pushViewController的引用。它现在有效吗?
-(void) Infobuttonpressed: (id) sender;
{ Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myView];
UIViewController *pushController = [[UIViewController alloc] init];
[self addSubview:pushController.view];
[pushController presentModalViewController:navController animated:YES];
[myView release];
}
答案 2 :(得分:0)
infoItem的目标设置为nil。您应该将其设置为定义Infobuttonpressed的类的实例。我假设那是mainviewController?
此外,要调用的选择器指定为:@selector(Infobuttonpressed),而该方法称为“Infobuttonpressed:”,即带有参数。一旦修复目标,这可能会导致运行时错误。
然后,你处理事物的方式有一些特点。正如Nik已经回答的那样,您需要选择是否要以模态方式呈现视图(使用presentModalViewController),或者将其作为导航堆栈的一部分呈现(使用pushViewController),您不能同时执行这两种操作。
如果您将其作为导航堆栈的一部分展示,那么它已经有一个导航栏,因此您不应该自己添加一个导航栏。只需设置self.navigationItem的属性。
有关详细信息,请查看View Controller Programming Guide。