adding interactive label to a window based iPhone application - objective-c

I am trying to add a variable label to a simple window base iPhone application.
I am using XCode 3.2.6.
The app is an exaple I downloaded from the web and is just as simple as this:
a label where the user can type some text
a submit button
When the user clicks on submit, the text in the label is recorded in a mysql database with a simple query (the db is on my local machine and handled with a php script).
The query works, I just would like to add the label to write the result of the query.
So I added this code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)];
[label setText:#"write some text"];
But it doesn't work.
Do I need to connect the label to some object in Interface Builder?
Thanks so much

i think you forget to add label to your view like this :
[self.view addSubView: label];
this will insert your label on your current view

UITextField sounds a better solution... With that, you can edit the message and set programmatically the text.
Doc: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html

You should better make the UILabel *label as the property of the AppDelegate.
And then in the AppDelegate.m , you can init the UILabel
_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)];
At the end of your code of Launch , you should add the _label to the window bring the _label to front
[self.window addSubView:_label]
and in AppDelegate you should public a method to set the text.
- (void)showText:(NSString *)text
{
[_label setText:text];
[self.window bringSubviewToFront:_label];
}
And In your view or viewControll(should improt your AppDelegate.h) , when you click the submit button
- (void)clickSubmitButton:(UIButton)button
{
// TODO : get your text string
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate showText:YOURTEXT];
}

SOLVED!
I missed the #synthesize label !

Related

Is it possible to Have a Searchbar in a UIViewController iOS 8?

I need to have a Searchbar inside a ViewController as the picture bellow. I don't wanna use UItableViewcontroller. Is it possible to have this searchbar that will help me to query in a NSMutableArray thanks to NSpredicate ?
My Goal
After Searching showing the results of the array in the View "white view".
Yes, It is possible.
You can create a search bar programmatically.
First,Add UISearchBarDelegate Protocol in .h (header) file.
#interface ViewController : UIViewController<UISearchBarDelegate>
Then, Create UISearchBar and add as subview in your view.
UISearchBar searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(30, 80, screen.size.width-60, 40)]; //give your frame x,y and width and height
searchBar.placeholder = #"Enter City Name";
searchBar.delegate = self;
//searchBar.hidden = NO;
[self.view addSubview:searchBar];
Now, Implement delegate methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar{
//your code when search button clicked
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
//your code when you are entering text in search bar
}

Recreating a UIView like FaceTime

I found an easy way to display the camera feed, but it shows above my XIB. How can I put the feed in a View to display under the buttons and background?
Here's my code.
//ViewController.m
FaceCamViewer *viewer3 = [[FaceCamViewer alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
viewer3.cameraType = AVCaptureDevicePositionFront;
AVCaptureSession *session = [[AVCaptureSession alloc] init];
viewer3.session = session;
viewer3.draggable = NO;
[viewer3 startFaceCam];
[self.view addSubview:viewer3];
How do I create a property to access this through the XIB?
https://github.com/ijason/FaceCamTest
You could add a simple UIView to your XIB file, place it where you like and create a property that belongs to it. (#property IBOutlet UIView *faceView;) Hook up the property with the View in Interface Builder and then do [self.faceView addSubview:viewer3];

Some time cannot hide iOS keyboard and show UIPickerView

This is my .h
xxxx : UIViewController<UITextFieldDelegate>
and my .m
UITextField *quarterPicker = [[[UITextField alloc] init] autorelease];
[quarterPicker setFrame:CGRectMake(150, 370, 60, 30)];
[quarterPicker setText: #"Q1"];
quarterPicker.delegate = self;
and i use
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"textFieldShouldBeginEditing");
[textField resignFirstResponder];
[textField addTarget:self action:#selector(selectQuarterPicker:) forControlEvents:UIControlEventTouchDown];
return NO; // Hide both keyboard and blinking cursor.
}
When i touch to my textField, i alway see this log textFieldShouldBeginEditing
But some time, my Picker doesn't show. I use : https://github.com/TimCinel/ActionSheetPicker to make Picker.
I find out that, when i swipe my textFiled, my picker will display.
How can i fix this?
UPDATE: i put my UITextFiled insite UIScrollView
You are adding the target for the touch event after the first touch is made. So only the second will work. Why add this event? You can simply show the picker inside shouldBeginEditing without adding a touch event
Something like
[self selectQuarterPicker:self]; //assuming the parameter is the sender as you commented

Why is my image not being passed to my ViewController?

I have my app set up so that there will be multiple image pickers, each with it's own array of images. The user will press which set they wish to view, select an image, and then the app will pop back to the Root Controller. I want the image they have selected to be added as a SubView to my main ViewController.
I'm having issues getting the main ViewController to detect the image selected though.
Here's how I am trying:
In my Image Picker:
EDIT I'm having an issue again. I have added a new viewController so the users will choose which set of array of images they want to see instead of going straight to the array of images. I changed the way I pop to the viewController since I am not going back to the RootViewController now.
- (IBAction)buttonClicked:(id)sender
{
UIButton *button = (UIButton *)sender;
_selectedImage = [_images objectAtIndex:button.tag];
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.viewController.stampedImage = _selectedImage;
NSLog(#"delegate.viewController = %#", delegate.viewController);
NSLog(#"delegate.viewController.stampedImage = %#", delegate.viewController.stampedImage);
NSLog(#"_selectedImage = %#", _selectedImage);
[delegate.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}
I've ran an NSLog here and the selectedImage is set to an image.
Here is what I am doing in my main ViewController where I want the image to appear. I am running this code in viewWillAppear:
//stampedImage = _imagePicker.selectedImage; --- Not sure if I need this line
_stampedImageView = [[UIImageView alloc] initWithImage:stampedImage];
NSLog(#"ViewController stampedImage = %#", stampedImage);
[_stampedImageView setFrame:CGRectMake(0, 0, 80.0, 80.0)];
[_stampedImageView setMultipleTouchEnabled:YES];
[_stampedImageView setUserInteractionEnabled:YES];
[imageView addSubview:_stampedImageView];
[_stampedImageView release];
I've ran an NSLog here but the stampedImage always returns as null.
The stampedImage is always coming back as null
I have the stampedImage set as a property and is being synthesized.
I have also set done an #class in the .h for my image picker class.
Any ideas? I'm stumped on this one for some reason.

adding subviews to UITableViewCell's content view makes cell unselectable

I want to add some static text to a UITableViewCell in a UITextView.
UITextView *addressField = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 75)];
[addressField setBackgroundColor:[UIColor clearColor]];
[addressField setFont:[UIFont fontWithName:#"HelveticaNeue" size:14]];
[addressField setContentInset:UIEdgeInsetsMake(0, 20, 0, 0)];
[addressField setEditable:NO];
[addressField setScrollEnabled:NO];
// change me later
[addressField setText:#"John Doe\n555 Some Street\nSan Francisco, CA, 00000"];
[cell.contentView addSubview:addressField];
[addressField release];
This works great but I this code makes the cell unselectable probably because the UITextView is covering the entire cell.
How can I work around this so that I can have both the UITextView and selectable cells?
btw, I could make the UITextView size a bit smaller but users would still not be able to select the cell if they touch the UITextView.
I think a slightly better way to do it is to create a tap gesture recognizer on the entire table. (For example in your viewDidLoad)
// gesture recognizer to make the entire cell a touch target
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(changeFocus:)];
[tableView addGestureRecognizer:tap];
[tap release];
Then you create a selector (changeFocus: in this case) to do the actual selecting.
- (void)changeFocus:(UITapGestureRecognizer *)tap
{
if (tap.state == UIGestureRecognizerStateEnded)
{
CGPoint tapLocation = [tap locationInView:self.tableView];
NSIndexPath* path = [self.tableView indexPathForRowAtPoint:tapLocation];
[self tableView:self.tableView didSelectRowAtIndexPath:path];
}
}
You can make your changeFocus method more elaborate to prevent selections or give focus to specific subviews of the selected indexPath.
I would adopt the following approach in order to keep interaction enabled with both the UITextView and the UITableViewCell.
Declare your controller class (a UITableViewController I guess ?) as UITexView delegate.
When you declare your UITextView, set the table view controller as it's delegate.
Implement one of the UITextViewDelegate methods (ex : - (void)textViewDidChangeSelection:(UITextView *)textView) in your table view controller .m file.
From within this method you can manipulate the targeted cell either with a custom code or by triggering the tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) delegate method through selectRowAtIndexPath:animated:scrollPosition:.
Your code might then look like :
In the table view controller .h file :
#interface MyTableViewController : UITableViewController <UITextViewDelegate> { ...
...
}
In the table view controller .m file :
UITextView *addressField = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 75)];
[addressField setDelegate:self];
...
Then implement this function for example (or any other suitable UITextViewDelegate function) :
- (void)textViewDidChangeSelection:(UITextView *)textView {
// Determine which text view triggered this method in order to target the right cell
...
// You should have obtained an indexPath here
...
// Call the following function to trigger the row selection table view delegate method
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]
}
Note that there are other alternatives like subclassing UITextView and deal with it's touch methods. I would recommend to use the possibilites offered by its delegate protocol though.
Note also that it might be handy to have your UITextView declared or at least referenced as an instance variable of the table view controller class. This will help you easily keep track of which addressField was hit and get the right indexPath.
[addressField setUserInteractionEnabled:NO];
I hope this helps you a bit:
[self.view insertSubview:TextView aboveSubview:TableView];
Or vice-versa based on your requirements.