我正在開發帶有最新SDK的iOS 5+應用程序,並使用自定義XIB
定製了UIView
。此代碼僅適用於iPhone Retina 4英寸模擬器
這是TopMenuView.h
:
#import <UIKit/UIKit.h>
@interface TopMenuView : UIView
@property (weak, nonatomic) IBOutlet UIButton *MenuButton;
@property (weak, nonatomic) IBOutlet UILabel *MenuLabel;
@property (weak, nonatomic) IBOutlet UIButton *SearchButton;
@property (weak, nonatomic) IBOutlet UIButton *ProfileButton;
@property (weak, nonatomic) IBOutlet UIButton *HomeButton;
@property (weak, nonatomic) UIViewController* parentController;
- (IBAction)MenuClicked:(id)sender;
- (IBAction)SearchClicked:(id)sender;
- (IBAction)ProfileClicked:(id)sender;
- (IBAction)HomeClicked:(id)sender;
+ (id)topMenuView;
@end
這是TopMenuView.m
:
#import "TopMenuView.h"
#import "SearchViewController.h"
#import "ProfileViewController.h"
#import "HomeViewController.h"
#import "SWRevealViewController.h"
@implementation TopMenuView
@synthesize parentController;
+ (id)topMenuView
{
TopMenuView* topView = [[[NSBundle mainBundle] loadNibNamed:@"TopMenuView"
owner:nil
options:nil] lastObject];
// make sure customView is not nil or the wrong class!
if ([topView isKindOfClass:[TopMenuView class]])
{
[topView setFrame:CGRectMake(0, 0, 320, 49)];
return topView;
}
else
return nil;
}
#pragma mark -
#pragma mark IBAction methods
- (IBAction)MenuClicked:(id)sender
{
[self.parentController.revealViewController revealToggle:sender];
}
- (IBAction)SearchClicked:(id)sender
{
SearchViewController* search =
[[SearchViewController alloc] initWithNibName:@"SearchViewController"
bundle:nil];
[self.parentController.revealViewController setFrontViewController:search];
}
- (IBAction)ProfileClicked:(id)sender
{
ProfileViewController* profile =
[[ProfileViewController alloc] initWithNibName:@"MyProfileViewController"
bundle:nil];
[self.parentController.revealViewController setFrontViewController:profile];
}
- (IBAction)HomeClicked:(id)sender
{
HomeViewController* home =
[[HomeViewController alloc] initWithNibName:@"HomeViewController"
bundle:nil];
[self.parentController.revealViewController setFrontViewController:home];
}
@end
而且在UIViewController
我這樣做是爲了顯示它:
- (void)viewDidLoad
{
[super viewDidLoad];
topMenuView = [TopMenuView topMenuView];
topMenuView.MenuLabel.text = @"Home";
topMenuView.parentController = self;
[self.view addSubview:topMenuView];
// Set the gesture to open the slide menu.
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
而且本所認爲:
此代碼僅適用於iPhone Retina 4英寸模擬器。在iPhone Retina 3.5英寸和iPhone模擬器上,它不起作用。
問題是,我點擊任何「按鈕」它什麼都不做。沒有觸摸裏面向上事件是拋出(我在IBAction方法內設置調試點)。
我還沒有在真正的iPhone上測試它,因爲我還沒有開發者許可證。
發生了什麼事?爲什麼它不適用於iPhone 3.5英寸?
我想如果我有另一個視圖覆蓋我的'TopMenuView',它不會在任何模擬器中工作。 – VansFannel
東西正在捕捉你的觸覺事件,這可能是另一種觀點。我認爲視圖不夠長,不足以覆蓋4英寸屏幕上的按鈕,但足夠長以覆蓋3.5英寸屏幕。 –