Master page concept in iPhone development
I am developing an iPhone application in which I need to ha开发者_StackOverflow中文版ve a control, let's say a button, in all of my views.
I was wondering if there is an equivalent concept for Asp.net master pages in iPhone development?
Sure, use a custom UIView or UIViewController class and subview all your views or view controllers from that.
1. Create a viewController class as TopViewController and delete already created view first then add a view from IB (drag drop) and connect to File's Owner (view) then set height and width what you want.
2. Create object of TopViewController like-
TopViewController *topVC=[[TopViewController alloc] initWithNibName:@"TopViewController" bundle:nil];
[self.view addSubView:topVC.view];
We can achieve it using following step. 1. Create a new file with Subclass of ‘UIView’ and name it ‘customButom’ 2. Add a UIView Interface file and name it customButtom.xib and link customButtom file with xib. 3. After this set the UIView size to Freedom in Utility Panel. And set your desired width and height. In this example, I have set it 160x50 I’ve created a protocol, properties and IBAction for the event handle.
//customButton.h
#import<UIKit/UIKit.h>
// createdcustomButtonDelegate protocol
@protocolcustomButtonDelegate<NSObject>
-(void)performButtonClicked;
@end
@interfacecustomButton : UIView
@property(nonatomic,retain)id<customButtonDelegate>delegate;
-(IBAction)customButtonClicked:(id)sender;
@end
//customButton.m Implemented properties and button action `
#import"customButton.h"
@implementationcustomButton
-(id)initWithFrame:(CGRect)frame {
self = [superinitWithFrame:frame];
if (self) {
UIView *buttonView =[[[NSBundlemainBundle]loadNibNamed:@"customButton"owner:selfoptions:nil] objectAtIndex:0];
[selfaddSubview:buttonView];
}
returnself;
}
-(IBAction)customButtonClicked:(id)sender {
if (self.delegate != nil) {
[self.delegateperformButtonClicked];
}
}
@end
// Viewcontroller.h Import customButton.h file in our view controller and add delegate `
#import<UIKit/UIKit.h>
#import"customButton.h"
@interfaceViewController : UIViewController<UIAlertViewDelegate,customButtonDelegate>
@end
// Viewcontroller.m file
@interfaceViewController ()
@end
@implementationViewController {
customButton *buttonView;
}
- (void)viewDidLoad {
[superviewDidLoad];
// we can display / set our custom view on specific location on view. just need to provide your desire Frame
//creating customButton object and added this object to our view.
buttonView = [[customButtonalloc]initWithFrame:CGRectMake(80, 465, 160, 50)];
buttonView.delegate = self;
[self.viewaddSubview:buttonView];
}
#pragma mark custombutton delegate
-(void)performButtonClicked {
NSLog(@"Perform whatever you want to");
}
@end
精彩评论