2012-11-26 51 views
3

我是新手,試圖使用UITextView來製作類似於iPhone的Notes應用程序的應用程序。 我得到textView和線路,它工作正常。如何以編程方式在其上添加UINavigationBar和後退按鈕

我的問題是,我想添加一個UINavigationBar和後退按鈕。 我想在底部添加UIToolBar,並在其上添加2個toolBarItems如何編程。任何幫助將是一個偉大的推升爲我..

下面是代碼段。

NoteView.h

@interface NoteView : UITextView <UITextViewDelegate,UITabBarControllerDelegate> 
{ 

} 

NoteView.m

- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame]; 

    if (self) { 
     self.backgroundColor = [UIColor colorWithRed:0.6f green:0.6f blue:1.0f alpha:1.0f]; 
     self.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:20]; 
     self.contentMode = UIViewContentModeRedraw; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor); 
    CGContextSetLineWidth(context, 1.0f); 

    CGContextBeginPath(context); 

    NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height)/ self.font.leading; 

    CGFloat baselineOffset = 6.0f; 
    for (int x = 0; x < numberOfLines; x++) { 
     CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset); 
     CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset); 
    } 

    CGContextClosePath(context); 
    CGContextStrokePath(context); 
} 

AddNotesViewController.h

@interface AddNotesViewController : UIViewController <UITextViewDelegate,UITabBarDelegate> 
{ 
    NoteView *note; 
} 

@property (nonatomic, retain) NoteView *note; 

@end 

AddNotesViewController.m

- (void)loadView 
{ 
    [super loadView]; 
    self.note = [[[NoteView alloc] initWithFrame:self.view.bounds] autorelease]; 
    [self.view addSubview:note]; 
    note.delegate = self; 
    [email protected]""; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    [note setNeedsDisplay]; 
} 

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    CGRect frame = self.view.bounds; 
    frame.size.height -= KEYBOARD_HEIGHT; 
    note.frame = frame; 
} 

- (void)textViewDidEndEditing:(UITextView *)textView 
{ 
    note.frame = self.view.bounds; 
} 

- (BOOL)textView:(UITextView *)textView 
shouldChangeTextInRange:(NSRange)range 
     replacementText:(NSString *)text 
{ 
    if ([text isEqualToString:@"\n"]) { 
     [textView resignFirstResponder]; 
     return NO; 
    } 
    return YES; 
} 

請告訴我如何以及在哪裏添加導航欄,返回按鈕和工具欄,2個toolBarItems上it.Thanks提前...

+0

重複。 [ButtonOnNav](http://stackoverflow.com/questions/2848055/add-button-to-navigationbar-programatically)&[NavBackButton](http://stackoverflow.com/questions/1441699/uinavigationcontroller-back-button-自定義文本)已經被回答。 通過[鏈接](http://stackoverflow.com/faq#dontask)和[F&Q](http://stackoverflow.com/faq#questions)。 – HDdeveloper

+0

http://stackoverflow.com/questions/13488710/how-to-set-a-picture-programmatically-in-a-navbar/13488781#13488781 – Rajneesh071

+0

是否使用導航控制器或不是 – Rajneesh071

回答

0

使用此代碼... 。

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] 
            initWithTitle:NSLocalizedString(@"Back", @"") 
            style:UIBarButtonItemStyleDone 
            target:self 
            action:@selector(YourActionMethod:)]; 


self.navigationItem.leftBarButtonItem = addButton; 
+0

我必須在AddNotesViewController.m中添加 - (void)loadView對嗎? – suvarna

+0

是............ – Venkat

+0

它不工作... – suvarna

3

導航欄圖像

UINavigationBar *navBar = [[self navigationController] navigationBar]; 
    UIImage *image = [UIImage imageNamed:@"TopBar.png"]; 
    [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 

後退按鈕

-(void)getBackBtn 
{ 
    UIButton *Btn =[UIButton buttonWithType:UIButtonTypeCustom]; 

    [Btn setFrame:CGRectMake(0.0f,0.0f,50.0f,30.0f)]; 
    [Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"back.png"]] forState:UIControlStateNormal]; 
    //[Btn setTitle:@"OK" forState:UIControlStateNormal]; 
    //Btn.titleLabel.font = [UIFont fontWithName:@"Georgia" size:14]; 
    [Btn addTarget:self action:@selector(backBtnPress:) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:Btn]; 
    [self.navigationItem setLeftBarButtonItem:addButton]; 
} 

BackButtonAction

-(IBAction)backBtnPress:(id)sender 
{ 
} 

在導航欄

查看查看上的導航欄,你可以按照我的回答Link

相關問題