Updated 26 October 2021
Sometimes we need to show the UIview over view controller for that we can use xib file to pop over any view controller and we can add any action on the xib file .
Here I have taken the example of a xib file where I have attached the button, label on xib file after clicking on it, it will remove and fetch the value whatever you passed.
follow some steps:
1: Take one Xib file .
2: Take one UILabel and one UIButton over it.
3: Take one cococatouch class file and their parent would be UIViewcontroller. ( Name as : abc.m,abc.h file)
4: Open Xib file and click on files’owner and on right side (Custome class) write file name ex: abc.m
5: Select the files’s owner click on ” Connection inspector” here you have to drag the UIView to View.
6: Now select View and drag the reference of UIButton , UILabel in abc.h class.
7: Write the abc.h file as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#import <UIKit/UIKit.h> @protocol LanguageListViewDelegate <NSObject> - (void)didSelectLanguage:(NSString *)lang; @end @interface LanguageChoose : UIViewController{ NSString *text; } @property (nonatomic, assign) id<LanguageListViewDelegate>delegate; - (id)initWithNibName:(NSString *)nibNameOrNil delegate:(id)delegate; @property (weak, nonatomic) IBOutlet UITextfield *textBox; |
8: write abc.m file as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#import "LanguageChoose.h" @interface LanguageChoose () @end @implementation LanguageChoose - (id)initWithNibName:(NSString *)nibNameOrNil delegate:(id)delegate { self = [super initWithNibName:nibNameOrNil bundle:nil]; if (self) { // Custom initialization _delegate = delegate; } return self; } - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)selectLanguage:(id)sender { if ([_delegate respondsToSelector:@selector(didSelectLanguage:)]) { [self.delegate didSelectLanguage:textBox.text]; [self dismissViewControllerAnimated:YES completion:NULL]; } else { NSLog(@"CountryListView Delegate : didSelectCountry not implemented"); } } |
9: Its time to call that xib file and fetch their data .
1 2 |
abc *cv = [[abc alloc] initWithNibName:@"LanguageChooseWindow" delegate:self]; [self presentViewController:cv animated:YES completion:NULL]; |
Note: here you have to call from any viewController class.
10 : write their delegate method so we can access their value.
1 2 3 4 |
- (void)didSelectLanguage:(NSString *)lang{ Nslog(@" %@", lang) } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.