iOS使用输入视图从自定义键盘中关闭模态视图

时间:2012-06-21 17:22:53

标签: ios

我有一个导航控制器,用textview显示模态视图

我还设置了自定义输入视图(带数字按钮的自定义键盘并输入按钮)

用于该textview

数字的按钮工作正常,但输入按钮不起作用(我去输入按钮取消模态视图)

我正在使用委托来执行此操作,但委托始终为nil

inputView.h

typedef enum ActionTag {
   ActionEnter = 0,
   ActionDivide,
   ActionMultiply,
   ActionSubtract,
   ActionAdd
} ActionTag;

@protocol InputViewDelegate <NSObject,UIInputViewAudioFeedback>

- (void)dismissv:(ActionTag)tag;
@end

@interface InputView : UITextView {
   UIView *inputView;
   id <InputViewDelegate> ivDelegate;

}



@property (nonatomic,weak) id <InputViewDelegate> delegate;


- (IBAction)takeInputFromTitle:(id)sender;
- (IBAction)doDelete:(id)sender;
- (IBAction)doTaggedAction:(id)sender;
@end







inputView.m

   #import "InputView.h"


@implementation InputView

- (BOOL) enableInputClicksWhenVisible {
   return YES;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (UIView *)inputView {
   if (!inputView) {
      NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"CKeybroad" owner:self options:nil];
      NSLog(@"loaded objects from nib: %@", objects);
      inputView = [objects objectAtIndex:0];
   }
   return inputView;
}

- (IBAction)takeInputFromTitle:(id)sender {

   //[[UIDevice currentDevice] playInputClick];

   self.text = [self.text stringByReplacingCharactersInRange:self.selectedRange
                                                  withString:((UIButton *)sender).currentTitle];

  // amount=self.text;

}

- (IBAction)doDelete:(id)sender {


   NSRange r = self.selectedRange;
   if (r.length > 0) {
      // the user has highlighted some text, fall through to delete it
   } else {
      // there's just an insertion point
      if (r.location == 0) {
         // cursor is at the beginning, forget about it.
         return;
      } else {
         r.location -= 1;
         r.length = 1;
      }
   }
  // self.text = [self.text stringByReplacingCharactersInRange:r withString:@""];
   self.text=@"";
   r.length = 0;
   self.selectedRange = r;

}

- (IBAction)doTaggedAction:(id)sender {

   ActionTag tag = [sender tag];
   [ivDelegate dismissv:tag];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

cashView.h
import <UIKit/UIKit.h>
#import "InputView.h"

@protocol ModalViewControllerDelegate <NSObject>
- (void)didDismissModalView;
@end

@interface cash_view : UIViewController <InputViewDelegate> {

   id <ModalViewControllerDelegate> delegate;
   UINavigationBar *navigationBar;
   IBOutlet InputView *inputView; 
   IBOutlet UILabel *amount;
}

@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar;
@property (nonatomic,retain) IBOutlet UILabel *amount;
@property (nonatomic, retain) id <ModalViewControllerDelegate> delegate;

- (void)closeModalViewController;
-(IBAction)changelabel:(id)sender;
-(IBAction)dismissView:(id)sender;




@end




cashView.m

#import "cash_view.h"

@implementation cash_view


@synthesize navigationBar;
@synthesize amount;
@synthesize delegate;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
      self.title = @"Cash Due";
   }
   return self;
}



- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [inputView becomeFirstResponder];
    inputView.delegate=self;
    amount.text=@"00.00";
    // Do any additional setup after loading the view from its nib.

   [self.navigationBar setItems:[NSArray arrayWithObject:self.navigationItem]];
    UIBarButtonItem * closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonSystemItemCancel target:self 
                                                                   action:@selector(closeModalViewController)];
   self.navigationItem.rightBarButtonItem = closeButton;
  navigationBar.barStyle = UIBarStyleBlack;

}
- (void)closeModalViewController {
   [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

- (void)doEnter {

[self dismissModalViewControllerAnimated:YES];


}

- (void)textViewDidChange:(UITextView *)textView {

   int length=inputView.text.length;
   if(length==0) 
    amount.text=@"0,00";
   else
      amount.text=inputView.text;

}

- (void) doArithmetic {

   // inputView.text = [decimalFormatter stringFromNumber:result];
  //inputView.text=@"1";
   self.amount.text=@"1";


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

  // [self dismissModalViewControllerAnimated:YES];
   [delegate didDismissModalView];   
}

- (void)doDecimalArithmetic:(SEL)method {



}
#pragma mark InputViewDelegate
- (void)dismissv:(ActionTag)tag {
   switch (tag) {
      case ActionEnter:
         [self doEnter];
         break;
      case ActionDivide:
         [self doDecimalArithmetic:@selector(decimalNumberByDividingBy:)];
         break;
      case ActionMultiply:
         [self doDecimalArithmetic:@selector(decimalNumberByMultiplyingBy:)];
         break;
      case ActionSubtract:
         [self doDecimalArithmetic:@selector(decimalNumberBySubtracting:)];
         break;
      case ActionAdd:
         [self doDecimalArithmetic:@selector(decimalNumberByAdding:)];
         break;
      default:
         break;
   }
}

// Done button clicked
- (IBAction)dismissView:(id)sender {
   // Call the delegate to dismiss the modal view
   [delegate didDismissModalView];
}

@end

0 个答案:

没有答案