I need to add pickers to my textboxes, but can't figure out how to get them to work. I've gone through a number of tutorials that show you how to start from scratch, which is kinda useless if you have an existing app and you're wanting to add functionality, for the most part they're not very helpful.
I'm looking for something very similar to the image below. The one displayed below is obviously a UIDatepicker, but ideally
I'm looking for a custom picker for "place", "term", and "gate".
So for now, skip the UIDatepicker item, and focus on the custom picker item.
One more thing, this is iOS7, and I am using storyboards AND arc!
I ran across this (http://www.youtube.com/watch?v=BtmQp5aP304) which was really helpful, but again it's starting from scratch. What I can't figure out is how to implement this within my existing code. So I'm looking for a walk-through, a tutorial that someone can point me at to get me started.
There is this on stackoverflow ->
how to pop up datePicker when text filed click and disappear when editing done
Which gave me a few ideas, but not exactly what I'm looking for, tried the suggestions and ended up with more errors than anything else.
Thanks.
#interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
#implementation ViewController
{
UIPopoverController *popoverController;
NSString *currentPick;
….
}
.
.
.
-(void)makePicker{
UIPickerView *picker = [[[UIPickerView alloc]init]autorelease];
picker.frame = CGRectMake(0, 50, 320, 1000);
picker.showsSelectionIndicator = YES;
picker.delegate = self;
UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[done setBackgroundImage:[UIImage imageNamed:#"Save.png"] forState:UIControlStateNormal];
[done setFrame:CGRectMake(260, 10, 50, 30)];
[done addTarget:self action:#selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
UIButton *cancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancel setBackgroundImage:[UIImage imageNamed:#"Cancel.png"] forState:UIControlStateNormal];
[cancel setFrame:CGRectMake(10, 10, 50, 30)];
[cancel addTarget:self action:#selector(cancelButton:) forControlEvents:UIControlEventTouchUpInside];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
UIActionSheet *pickerAction = [[[UIActionSheet alloc]initWithTitle:[cells objectAtIndex:indexPath.section] delegate:self cancelButtonTitle:nil destructiveButtonTitle:Nil otherButtonTitles:nil]autorelease];
[pickerAction addSubview:picker];
[pickerAction addSubview:done];
[pickerAction addSubview:cancel];
[pickerAction showInView:self.view];
pickerAction.frame = CGRectMake(0,[UIScreen mainScreen].bounds.size.height - 330,320, 350);
}else
{
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
[popoverView addSubview:picker];
[popoverView addSubview:done];
[popoverView addSubview:cancel];
[popoverContent.view addSubview:popoverView];
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:<your frame> inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 260);
[popoverView release];
[popoverContent release];
}
}
.
.
.
// mainArray here is a NSArray of several arrays, these arrays contain the titles for different selections… e.g. #[fontArray,colourArray,nameArray]
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [[mainArray objectAtIndex:clickedAt] count];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [[mainArray objectAtIndex:clickedAt] objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
currentPick = [[mainArray objectAtIndex:clickedAt] objectAtIndex:row];
}
- (void)doneButton:(UIButton *)button
{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
UIActionSheet *as = (UIActionSheet *)[button superview];
[as dismissWithClickedButtonIndex:0 animated:YES];
}
else
[popoverController dismissPopoverAnimated:YES];
// save your selections here using currentPick object
}
- (void)cancelButton:(UIButton *)button
{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
UIActionSheet *as = (UIActionSheet *)[button superview];
[as dismissWithClickedButtonIndex:0 animated:YES];
}
else
[popoverController dismissPopoverAnimated:YES];
}
Enjoy your Picker ;)
check this, make a full screen UIView , a UIImageView , a UIPickerView and two UIButtons (save and cancel)in .xib then
make their outlets in your .h file:
#property (retain, nonatomic) IBOutlet UIView *bg_view;
#property (retain, nonatomic) IBOutlet UIImageView *bg_customImageView;
#property (retain, nonatomic) IBOutlet UIPickerView *myPicker;
- (IBAction)cancelOut:(UIButton *)sender;
- (IBAction)saveOut:(UIButton *)sender;
#property (retain, nonatomic) IBOutlet UIButton *saveButton;
#property (retain, nonatomic) IBOutlet UIButton *cancelButton;
then in .m file:
-(void)customPickerShow
{
[self.bg_view setBackgroundColor:[UIColor colorWithRed:0.3 green:0.25 blue:0.05 alpha:0.5]];
[self.bg_view setHidden:NO];
self.navigationController.toolbarHidden = YES;
self.navigationController.navigationBarHidden = YES;
[self showMyPicker];
}
-(void)showMyPicker
{
[self.myPicker reloadAllComponents];
[self animatePicker];
}
-(void)animatePicker
{
self.cancelButton.frame = CGRectMake((framewidth/2) - 130, frameheight, 70, 1);
self.saveButton.frame = CGRectMake((framewidth/2) + 60, frameheight, 70, 1);
self.bg_customImageView.frame = CGRectMake((framewidth/2) - 140, frameheight, 280, 1);
[UIView beginAnimations:Nil context:Nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
self.bg_customImageView.frame = CGRectMake((framewidth/2) - 150, frameheight - 230, 300, 300);
self.cancelButton.frame = CGRectMake((framewidth/2) - 130, frameheight - 220, 50, 50);
self.saveButton.frame = CGRectMake((framewidth/2) + 80, frameheight - 220, 50, 50);
[UIView commitAnimations];
}
- (IBAction)cancelOut:(UIButton *)sender {
self.navigationController.navigationBarHidden = NO;
self.navigationController.toolbarHidden = NO;
[self.bg_view setHidden:YES];
}
- (IBAction)saveOut:(UIButton *)sender {
// save here your selections
self.navigationController.navigationBarHidden = NO;
self.navigationController.toolbarHidden = NO;
[self.bg_view setHidden:YES];
}
call this [self customPickerShow]; function to make it work..
This will look like action sheet with picker and buttons.. you can customize with your choice of background image and your choice of frame in animatePicker function
Don't forget to make this bg_view at top of other views and make it sethidden by default
Purely using UIStoryboard probably won't be able to either solve the project, or not the best way. This is my solution.
1) Create a CustomPicker
2) Define your Datasource and Delegate
3) Attached the CustomPicker to the inputView of the target TextField.
4) you can set it up at the delegate method
-(Bool)textFieldShouldBeginEditing:(UITextField *)textField
In this approach, we add the Picker by coding it (not by UIStoryboard).
I have this demo project to show this purpose.
Related
I have a UIViewController with a SegmentedControl, UITableView and a UISearchController. The SegmentedControl is at the top of the main View with the tableView just beneath it. The searchController's searchBar is placed in the tableView.tableHeaderView and looks like this:
When the searchBar is tapped (made active) it moves down leaving a gap just above:
Also, if the searchBar is active and then the segmentedConrol is tapped (filtering the table data and reloading the tableView) then the tableView loads but with a gap at the top. (I have purposely set the searchBar to hidden when the 'Category' filter is selected.
If the segmentedControl 'Category' is selected when the searchBar is not active this is how it looks (and should look):
I need two things (I think they are related), 1) for the searchBar to NOT move when active and 2) for the searchBar to not be present when 'Category' is selected and for the tableView to have no gap at the top.
.h:
#interface ExhibitorViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>
{
// DATA
NSMutableArray *arrayOfExhibitors;
NSMutableArray *arrayOfExhibitorsFiltered;
NSMutableArray *arrayOfCategories;
NSMutableArray *arrayOfCategoriesFiltered;
// VARS
int selectedSegment;
float searchBarHeight;
float tableViewY;
NSString *currentCategory;
CGRect tableViewStartRect;
// UI
UISegmentedControl *segmentedControl;
UIView *categorySelectedView;
UIView *headerView;
}
#property (nonatomic, strong) UITableView *tableView;
#property (nonatomic, strong) UISearchController *searchController;
#property (nonatomic, readonly) NSArray *searchResults;
#property (strong, nonatomic) NSString *sponsorsOnly;
#end
.m:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:NO];
if (selectedSegment == 0) {
self.searchController.searchBar.hidden = FALSE;
}
if (!_searchController.searchBar.superview) {
self.tableView.tableHeaderView = self.searchController.searchBar;
}
}
-(void)loadTableView
{
[self printStats:#"loadTableView START"];
searchBarHeight = self.searchController.searchBar.frame.size.height;
Settings *settingsInstance = [Settings new];
if(!_tableView) {
segmentedControl = [UISegmentedControl new];
segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"Exhibitor", #"Category", nil]];
[segmentedControl setFrame:CGRectMake(0, 0, self.view.frame.size.width, 35)];
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue) forControlEvents:UIControlEventValueChanged];
self.automaticallyAdjustsScrollViewInsets = YES;
self.edgesForExtendedLayout = UIRectEdgeNone;
self.searchController.hidesNavigationBarDuringPresentation = NO;
//self.definesPresentationContext = NO;
float tvX = self.view.frame.origin.x;
float tvY = self.view.frame.origin.y + segmentedControl.frame.size.height;
float tvWidth = self.view.frame.size.width;
float frameHeight = self.view.frame.size.height;
float tvHeight = self.view.frame.size.height - segmentedControl.frame.size.height;
tableViewStartRect = CGRectMake(tvX, tvY, tvWidth, tvHeight);
_tableView = [UITableView new];
_tableView = [[UITableView alloc] initWithFrame:tableViewStartRect];
//_tableView.contentInset = UIEdgeInsetsMake(0, 0, 44, 0);
_tableView.separatorColor = [UIColor clearColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:segmentedControl];
[self.view addSubview:_tableView];
[_tableView setTag:1];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
}
if (!categorySelectedView) {
float levelOneStart = (0);
categorySelectedView = [[UIView alloc] initWithFrame:CGRectMake(0, levelOneStart, self.view.frame.size.width, (screenHeight * 0.05))];
[categorySelectedView setBackgroundColor:[UIColor grayColor]];
[categorySelectedView setTag:4];
MyLabel *catSelectedLabel = [[MyLabel alloc] initWithFrame:categorySelectedView.frame];
[catSelectedLabel setFont:[UIFont systemFontOfSize:[settingsInstance getFontSizeFor:#"Label"]]];
[catSelectedLabel setTag:5];
[catSelectedLabel setBackgroundColor:[UIColor lightTextColor]];
[catSelectedLabel setTextColor:[UIColor darkGrayColor]];
UIButton *categoryBackButton = [[UIButton alloc] initWithFrame:CGRectMake((screenWidth * 0.6), levelOneStart, (screenWidth * 0.4), (screenHeight * 0.05))];
[categoryBackButton setTitle:#"^ Back ^" forState:UIControlStateNormal];
[categoryBackButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[categoryBackButton addTarget:self action:#selector(resetTableViewCategories) forControlEvents:UIControlEventTouchUpInside];
[catSelectedLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(resetTableViewCategories)]];
[categoryBackButton.titleLabel setFont:[UIFont systemFontOfSize:[settingsInstance getFontSizeFor:#"Label"]]];
[categorySelectedView addSubview:catSelectedLabel];
[categorySelectedView addSubview:categoryBackButton];
[categorySelectedView setHidden:TRUE];
}
if (!headerView) {
headerView = [[UIView alloc] initWithFrame:CGRectMake(0, (0), screenWidth, (searchBarHeight))];
[headerView addSubview:categorySelectedView];
[self.view addSubview:headerView];
[headerView setBackgroundColor:[UIColor purpleColor]];
[self.view sendSubviewToBack:headerView];
}
[self.view setTag:11];
tableViewY = _tableView.frame.origin.y;
[self printStats:#"loadTableView END"];
}
-(UISearchController*)searchController
{
if (!_searchController) {
_searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.searchBar.delegate = self;
[_searchController.searchBar sizeToFit];
}
return _searchController;
}
-(void)segmentedControlHasChangedValue
{
[self.searchController setActive:NO];
if ((segmentedControl.selectedSegmentIndex == 0)) {
selectedSegment = 0;
currentCategory = #"";
[self resetTableViewExhibitors];
[_tableView setContentOffset:CGPointMake(0, -1) animated:NO];
} else {
selectedSegment = 1;
[self resetTableViewCategories];
[_tableView setContentOffset:CGPointMake(0, -1) animated:NO];
//[_tableView setContentOffset:CGPointMake(0, 56) animated:NO];
[_tableView setTableFooterView:nil];
}
[_tableView reloadData];
}
I have tried changing the insets of various views and forcing a manual changes to the frames of various views (this is the closest thing to a fix but seems very hacky). What am I doing wrong?
Edit: Have also tried :
-(void)segmentedControlHasChangedValue
{
[self.searchController setActive:NO];
if ((segmentedControl.selectedSegmentIndex == 0)) {
selectedSegment = 0;
currentCategory = #"";
[self resetTableViewExhibitors];
[_tableView setContentOffset:CGPointMake(0, -1) animated:NO];
} else {
selectedSegment = 1;
[_searchController dismissViewControllerAnimated:NO completion^() {
[self resetTableViewCategories];
[_tableView setContentOffset:CGPointMake(0, -1) animated:NO];
[_tableView setTableFooterView:nil];
}];
}
[_tableView reloadData];
}
Because you use UISearchController so searchBar will always move when it actives. To avoid it, use UISearchBar. And when you use UISearchBar, it's easy to hide when you select Category tab
When I bring up a UIImagePickerController and then close it, it duplicates the content in my modal window. Below are the before and after pictures:
Here's the code that shows the image picker:
-(void) choosePhotos
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];
[imagePicker setAllowsEditing:YES];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagePicker animated:YES completion:nil];
}
Here's the rest of my code (if needed):
-(id) init
{
self = [super init];
if (self)
{
[self.navigationItem setTitle:#"Deposit"];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleDone target:self action:#selector(cancel)];
[self.navigationItem setLeftBarButtonItem:closeButton];
toItems = #[#"Account...5544", #"Account...5567"];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[self.view addGestureRecognizer:recognizer];
}
return self;
}
-(void) hideKeyboard
{
for (UITextField *field in [scrollView subviews])
{
[field resignFirstResponder];
}
}
-(void) cancel
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [toItems count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [toItems objectAtIndex:row];
}
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.view setBackgroundColor:[UIColor whiteColor]];
UILabel *toLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 50, 100)];
[toLabel setText:#"To:"];
toPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(130, -30, 220, 100)];
[toPicker setDataSource:self];
[toPicker setDelegate:self];
UILabel *amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 70, 100)];
amountLabel.lineBreakMode = NSLineBreakByWordWrapping;
amountLabel.numberOfLines = 0;
[amountLabel setText:#"Check Amount:"];
UITextField *amountField = [[UITextField alloc] initWithFrame:CGRectMake(130, 100, 270, 100)];
[amountField setPlaceholder:#"Enter Amount"];
[amountField setReturnKeyType:UIReturnKeyDone];
[amountField setKeyboardType:UIKeyboardTypeDecimalPad];
UILabel *imagesLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 70, 100)];
imagesLabel.lineBreakMode = NSLineBreakByWordWrapping;
imagesLabel.numberOfLines = 0;
[imagesLabel setText:#"Check Images:"];
UIButton *imagesButton = [[UIButton alloc] initWithFrame:CGRectMake(120, 200, 244, 99)];
[imagesButton setBackgroundImage:[UIImage imageNamed:#"photos.png"] forState:UIControlStateNormal];
[imagesButton addTarget:self action:#selector(choosePhotos) forControlEvents:UIControlEventTouchUpInside];
CGRect bounds = [[UIScreen mainScreen] bounds];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
[scrollView setAlwaysBounceVertical:YES];
[scrollView setShowsVerticalScrollIndicator:YES];
[scrollView addSubview:toLabel];
[scrollView addSubview:toPicker];
[scrollView addSubview:amountLabel];
[scrollView addSubview:amountField];
[scrollView addSubview:imagesLabel];
[scrollView addSubview:imagesButton];
[self.view addSubview:scrollView];
}
I recommend you use viewDidLoad as the place to create and add your views:
- (void)viewDidLoad {
[super viewDidLoad];
//init and add your views here
//example view
self.someLabel = [[UILabel alloc] init];
self.someLabel.text = #"someExampleText";
[self.view addSubview:self.someLabel];
}
And either viewWillAppear or viewDidLayoutSubviews as the place to configure their sizes (i prefer viewDidLayoutSubviews so i'll use it as an example):
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.someLabel.frame = CGRectMake(kMargin,kMargin,kLabelWidth,kLabelHeight);
}
Of course, in order to do this you need to have a reference to all the views you wish to configure this way by creating a property to them in the interface:
#interface YourViewController ()
#property (nonatomic, strong) UILabel *someLabel;
#end;
static CGFloat const kMargin = 20.0f;
static CGFloat const kLabelHeight = 30.0f;
static CGFloat const kLabelWidth = 100.0f;
Also, it is recommended you avoid using hard coded values for their sizes (doing it like CGRectMake(20,20,100,70) but this it not completely wrong.
Not using hard coded values does not mean setting them yourself, it just means to make their values more readable (and on most cases, dynamic).
In my example, i created kMargin, kLabelHeight and kLabelWidth, meaning that anyone who looks at this code will understand what they mean, they will know what to change if needed, and these values can be used in other places.
For example, you could have 4 labels, and in order to keep them all following the same layout rules, all of them will use the kMargin value on the origin.x.
You could also, instead of using a static value for the width, you can implement a dynamic value, like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat labelWidth = self.view.bounds.size.width - (kMargin * 2);
self.someLabel.frame = CGRectMake(kMargin,kMargin,labelWidth,kLabelHeight);
}
What i did here is to make my label to have the same width as my super view, but i made it account for the left and the right margins (by taking the total view width and reducing twice the margin value).
Since we are doing this on the viewDidLayoutSubviews method, which gets called whenever the superview changes its size (for example, orientation change) this will ensure your UILabel can be shown on any size of view and orientation without extra code to handle 'specific cases'.
Your UI elements are being added to your view every time viewWillAppear is called. This is called when your image picker dismisses and returns to your view, so they're being duplicated. Either check to see whether your UI elements already exist before creating them again, or do your UI setup in the viewDidLoad method which will only be run once. You could try this perhaps, using a BOOL property to keep track:
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
if (!self.alreadyAppeared) {
self.alreadyAppeared = YES;
// Create labels and buttons
}
}
created a class,
GSSFeatureScreenLocationTabView : UIView,
to add multiple view into a scroll view,
the view comes up with buttons which are linked to a selector that is supposed to push to another view controller, It sends the placeID back up but from some reason doesn't push to the next view controller
GSSFeatureScreenLocationTabView.h
#import "featureScreenViewController.h"
#interface GSSFeatureScreenLocationTabView : UIView
{
UIImageView* imageView;
UILabel* titleLabel;
UILabel* distanceLabel;
UILabel* descriptionLabel;
UIButton* pushButton;
}
#property (nonatomic) NSString* placeID;
-(void)setLocationInfo:(NSString*)ID title:(NSString*)title distance:(double)distance description:(NSString*)description imageURL:(NSString*)imageURL;
#end
GSSFeatureScreenLocationTabView.m
#import "GSSFeatureScreenLocationTabView.h"
#import "GSSLocationDetailViewController.h"
#implementation GSSFeatureScreenLocationTabView
#synthesize placeID = _placeID;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 144, 140)];
titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 148, 107, 21)];
distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 177, 107, 21)];
descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 206, 107, 91)];
pushButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 144, 317)];
[titleLabel setText:#""];
[distanceLabel setText:#""];
[descriptionLabel setText:#""];
[descriptionLabel setLineBreakMode:NSLineBreakByWordWrapping];
[pushButton setTitle:#"" forState:UIControlStateNormal];
[pushButton addTarget:self action:#selector(pushToLocation) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:imageView];
[self addSubview:titleLabel];
[self addSubview:distanceLabel];
[self addSubview:descriptionLabel];
[self addSubview:pushButton];
}
return self;
}
-(void)setLocationInfo:(NSString *)ID title:(NSString *)title distance:(double)distance description:(NSString *)description imageURL:(NSString *)imageURL
{
_placeID = ID;
[titleLabel setText:title];
[distanceLabel setText:[NSString stringWithFormat:#"%f", distance]];
[descriptionLabel setText:description];
UIImage* image = [UIImage imageNamed:imageURL];
[imageView setImage:image];
}
-(void) pushToLocation
{
NSLog(#"%s", __FUNCTION__);
featureScreenViewController* fsvc = [[featureScreenViewController alloc]init];
[fsvc pushToLocationWithID:_placeID];
}
#end
in featureScreenViewController.m
- (void)viewDidLoad
{
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
if (isiPhone5)
{
// this is iphone 4 inch
[scroller setFrame:CGRectMake(0, 251, 320, 317)];
}
else
{
[scroller setFrame:CGRectMake(0, 251, 320, 229)];
}
[self addLocationViews];
}
-(void)addLocationViews
{
int count = (int)[_placeArray count];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(152*count, 317)];
for (int i = 0; i<count; i++) {
_placeObject = [_placeArray objectAtIndex:i];
GSSFeatureScreenLocationTabView * view = [[GSSFeatureScreenLocationTabView alloc]initWithFrame:CGRectMake((152*i), 0, 144, 317)];
[view setLocationInfo:[_placeObject placeID] title:[_placeObject placeName] distance:[_placeObject distacne] description:#"description" imageURL:#"noImage.png"];
[scroller addSubview:view];
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = YES;
}
-(void) pushToLocationWithID:(NSString*)placeID
{
NSLog(#"\n%s \nplaceID:%#\n\n", __FUNCTION__, placeID);
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
GSSLocationDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"GSSLocationDetailViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
the function -(void) pushToLocationWithID:(NSString*)placeID is called and the log shows the function and placeID, However it doesn't push to the location view controller.
tried creating a button just to link a segue to it and called the performSegue with identifier, doesn't reach prepare for segue and still doesn't push.
However using the same code from another page in an IBAction and it works. Is there someway to call super or something so it goes into the navController and pushes it ?
If i do presentViewController just to test modal like shown above it shows
Warning: Attempt to present <GSSLocationDetailViewController: 0xab85110> on <featureScreenViewController: 0xab9c230> whose view is not in the window hierarchy!
but if i do
[self.navigationController pushViewController:vc animated:YES];
it doesn't show anything in the log and doesn't push.
made a new project with same concept at http://www.narula-tech.com/testScrollPush.zip
You are in a UIView (GSSFeatureScreenLocationTabView) and you are allocating a new viewController (VC2: featureScreenViewController) from there and you are pushing another viewController (VC3: GSSLocationDetailViewController) from there.
But actually your view is in another viewController VC1 and you need to push VC3 from there and not from VC2 which you are allocating but is not "in the scene" or better, in the window hierarchy.
So you should control touches from the viewController and not from the UIView..for this reason the name: view**CONTROLLER**.
Another thing but not related:
name of class should began with the first letter capitol:
So not:
featureScreenViewController* fsvc;
but:
FeatureScreenViewController* fsvc;
I have been trying to incorporate a UIView/Toolbar above my keyboard but have had no luck. When I added a toolbar it was scrambled so thus I need to put it into a UIView but the UIView does not want to appear above the keyboard. Code Below:
My Header:
#property (nonatomic, Strong) IBOutlet UITextView *textView;
#property (nonatomic, strong) IBOutlet UIToolbar *TitleBar;
#property (nonatomic, weak) IBOutlet UIView *AddView;
The ViewDidLoad:
- (void)viewDidLoad
{
// observe keyboard hide and show notifications to resize the text view appropriately
/*[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
*/
if ([self respondsToSelector:#selector(setNeedsStatusBarAppearanceUpdate)]) {
// iOS 7
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
} else {
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
self.attributionTitle.delegate = self;
self.attribution.delegate = self;
textView.scrollEnabled = YES;
// quoteText.layer.borderColor = [UIColor blackColor].CGColor;
// quoteText.layer.borderWidth = 1.0f;
// textView.delegate = self; // code or in IB
[textView becomeFirstResponder];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
The textViewDidBeginEditing:
-(void)textViewDidBeginEditing:(UITextView *)textView
{
self.textView.inputAccessoryView = self.AddView;
}
Here is to show the UIView is connected:
I added the textView.inputAccessoryView = AddView;to the ViewDidLoadthen deleted the view from my storyboard and remade it. Lastly I added the UIView to the bottom black bar.
Adding the inputAccessoryView in textViewDidBeginEditing is probably too late. The input accessory view should be set before that, e.g., in the viewDidLoad method.
Try something like:
-(void)viewDidLoad{
[super viewDidLoad];
UIView
myTextField.inputAccessoryView = [self accessoryViewWithPreviousEnabled:NO nextEnabled:YES];
// more stuff as required...
}
And a method for creating a previous/next button (you'll need to provide your own images for the buttons and implements the previousAccessoryViewButtonTapped: and previousAccessoryViewButtonTapped: methods). It takes two BOOL parameters to indicate if the previous and/or next buttons should be enabled.
#pragma mark - Accessory view methods
-(UIView *)accessoryViewWithPreviousEnabled:(BOOL)previousEnabled nextEnabled:(BOOL)nextEnabled{
previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
previousButton.frame = CGRectMake(10, 2, 60, 30);
[previousButton setImage:[UIImage imageNamed:PREVIOUS_BUTTON] forState:UIControlStateNormal];
previousButton.enabled = previousEnabled;
[previousButton addTarget:self action:#selector(previousAccessoryViewButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
nextButton.frame = CGRectMake(80, 2, 60, 30);
[nextButton setImage:[UIImage imageNamed:NEXT_BUTTON] forState:UIControlStateNormal];
nextButton.enabled = nextEnabled;
[nextButton addTarget:self action:#selector(nextAccessoryViewButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIView *transparentBlackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 34)];
transparentBlackView.backgroundColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:0.6f];
UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 34)];
[accessoryView addSubview:transparentBlackView];
[accessoryView addSubview:previousButton];
[accessoryView addSubview:nextButton];
return accessoryView;
}
Note this method is hard coded for an iPad in landscape orientation. You need to change it for an iPhone.
The problem is that your self.AddView is already in your interface (because you put it there, in the storyboard). It can't be in two places at once.
hi i'm creating my own customized ComboBox, i copy on here my code so that anyone can use it. ( i don't know where else to put it lol )
I'd like also to know if you have better idea to improve it.
I get a small error, when i init the instance with the initWithBlurredImage, everything works fine but the blurred image doesn't cover the whole screen, it misses a small frame of about 5px. can you understand why?
//
// UIComboBox.h
// testComboBox
//
// Created by StabiloCode on 09/09/13.
// Copyright (c) 2013 SimplyXcode. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface UIComboBox : UIViewController <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource> {
UITextField *myTextField;
UIImageView *imageView;
UIPickerView *pickerView;
NSMutableArray *array;
UIToolbar *toolBar;
CGRect oldFrame;
UIImage *blurImage;
UIImageView *backgroundView;
}
#property (nonatomic, retain) UITextField *myTextField;
#property (nonatomic, retain) UIImageView *imageView;
#property (nonatomic, retain) UIPickerView *pickerView;
#property (nonatomic, retain) NSMutableArray *array;
#property (nonatomic, retain) UIImage *blurImage;
-(void)comboBox:(UIComboBox *)comboBox dataSource:(NSMutableArray *)anArray;
-(UIImage *)createBlur;
-(id)initwithBlurredImage:(UIImage *)anImage;
#end
//
// UIComboBox.m
// testComboBox
//
// Created by StabiloCode on 09/09/13.
// Copyright (c) 2013 SimplyXcode. All rights reserved.
//
#import "UIComboBox.h"
#interface UIComboBox ()
#end
#implementation UIComboBox
#synthesize myTextField, imageView, array, pickerView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
-(id)init {
self = [super init];
if ( self ){
}
return self;
}
-(id)initwithBlurredImage:(UIImage *)anImage{
blurImage = anImage;
NSLog(#"blur = %#",blurImage);
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
myTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 300, 30)];
[myTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:myTextField];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(myTextField.frame.size.width-20, myTextField.frame.size.height-17, 25, 25)];
UIImage *arrow = [UIImage imageNamed:#"arrow.png"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
[imageView setImage:arrow];
[self.view addSubview:imageView];
[myTextField setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[self showPickerView];
}
-(void)showPickerView{
//saving old frame to fix the position after the selection
oldFrame = self.view.frame;
//changing the frame to have the whole screen for the comboBox
[self.view setFrame:CGRectMake(0, 0, 320, 460)];
if(blurImage){
backgroundView = [[UIImageView alloc] initWithImage:blurImage];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:blurImage];
[backgroundView setFrame:CGRectMake(0,0, 320, 460)];
[backgroundView setContentMode:UIViewContentModeScaleToFill];
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
}
if ([myTextField.text isEqual: #""]){
myTextField.text = [array objectAtIndex:0];
}
[myTextField setEnabled:NO];
//creating the pickerview
pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 244, 320, 216)];
[pickerView setDataSource:self];
[pickerView setDelegate:self];
[pickerView showsSelectionIndicator];
[pickerView setShowsSelectionIndicator:YES];
[self.view addSubview:pickerView];
toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 200, 320, 244)];
toolBar.barStyle = UIBarStyleBlackTranslucent;
[toolBar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:#[space,cancelButton, doneButton]];
[self.view addSubview:toolBar];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [array objectAtIndex:row];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; }
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [array count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
myTextField.text = [array objectAtIndex:row];
}
-(void)done{
[toolBar removeFromSuperview];
[pickerView removeFromSuperview];
[myTextField setEnabled:YES];
[self.view setFrame:oldFrame];
if(blurImage){ [backgroundView removeFromSuperview]; }
}
-(void)cancel{
myTextField.text = #"";
[toolBar removeFromSuperview];
[pickerView removeFromSuperview];
[myTextField setEnabled:YES];
[self.view setFrame:oldFrame];
if(blurImage){ [backgroundView removeFromSuperview]; }
}
// setting the pickerView content
-(void)comboBox:(UIComboBox *)comboBox dataSource:(NSMutableArray *)anArray{
self.array = anArray;
}
#end
if you want to use the code, you're free to do that, just let me know if it works fine :) stabilocode#gmail.com