How to assign multiple UIButtons to a UITextView - iphone

I want to assign two UIButtons to UITextView so that when one of the buttons is pressed the textview content should change from what it had when the previous button was pressed.

MyViewController.h:
#interface MyViewController : UIViewController
{
NSString* savedText;
}
#property(nonatomic, retain) IBOutlet UITextView* textView;
- (IBAction)buttonOnePressed:(id)sender;
- (IBAction)buttonTwoPressed:(id)sender;
#end
Connect the view controller's textView outlet to the text view in the xib file. Connect the buttons to the corresponding actions. (See Apple's Xcode tutorial if you don't know how to do this.)
MyViewController.m:
- (void)buttonOnePressed:(id)sender
{
[savedText release];
savedText = [textView.text copy];
}
- (void)buttonTwoPressed:(id)sender
{
textView.text = savedText;
}
(These aren't the complete files, of course, just the bits to make it do what you're asking.)

Related

How to send data from UItextfields of one viewcontroller to new view controller? [duplicate]

I am a bit stuck on trying to pass data from two UITextfields on one viewcontroller to another viewcontroller. Basically I got the items below:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UITextfield *Textfield1;
IBOutlet UITextfield *Textfield2;
}
-(IBAction)GoNextView:(id)sender;
#end
ViewController.m
#interface ViewController ()
#end
#implementation ViewController
(IBAction)GoNextView:(id)sender {
SecondView *second = [[SecondView alloc] initWithNibName:nil bundle:nil];
[self presentViewController:second animated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
SecondView.h
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController {
IBOutlet UILabel *ShowTextfield1;
IBOutlet UILabel *ShowTextfield2;
}
-(IBAction)GoBack:(id)sender;
#end
SecondView.m
#interface SecondView ()
#end
#implementation SecondView
- (IBAction)GoBack:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}
What I want is, that when the button GoNextView is pressed it should pass the data in the UITextfield to the SecondView. And the go to the next page. The data should now be displayed in the two Labels on the SecondView. I've been struggling with this for many hours now and its killing me. I know I have to use some NSString and #property(nonatomic, retain) NSString *asdas. But I simply can't make it work. So based on the above code how do I pass data from the two UITextfield to the two labels on the next view?
Instead of navigating to the second view through a function, use Storyboard Segues. Create a storybaord application. To Create a segue you need a navigation view controller. You can do that by the following steps.
Select the first view controller and go to Editor -> Embed In -> Navigation Controller.
Then Ctrl+Click on the "GoNextView" button, and drag and drop in the SecondView.
You will get three options: push, model, custom. Click push then a segue will be created between these two views.
Click that segue and click the "Show the Attribute Inspector" on the right side property window. Now name it as "connector" or which ever name you prefer.
Now, in SecondView.h create two strings,
#property (nonatomic, retain) NSString *str1;
#property (nonatomic, retain) NSString *str2;
and synthesize them in SecondView.m.
Lets name your UITextFields as "text1" and "text2" which are in your ViewController. Go to the ViewController.h and add #import "SecondView.h"
In ViewController.m do the following,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"connector"])
{
ViewController *VC = [segue destinationViewController];
VC.str1 = text1.text;
VC.str2 = text2.text;
}
}
Now you can display these strings in your SecondView. In Secondview.m,
label.text = str1;
and it will be displayed.
Use NSUserDefaults or the normal synthesising common interface variables. For example,
NSUserDefaults
From sender's view controller,
NSString *text=textField1.text;
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
[prefs setObject:text forKey:#"text1value"];
And you can get the value from any view controller as,
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
NSString *received string= [prefs stringForKey:#"text1value"];
Normal assigners,
I believe you must be using performsegue to navigate view controllers. There is a delegate method called "prepareforsegue" and you can assign the textfield values into the interface variables. For example,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"seguename"]) {
destViewController *destViewController = segue.destinationViewController;
destViewController.name = #"some text";
}
}
In the receiver's view controller, getter variable is actually defined as,
In .h file, #property(nonatomic, strong) NSString *name;
In .m file, Don't forget to synthesis the variable after implementation
#implementation destViewController
#synthesize name;
Hope it helps!

Multiple images in UITableViewCell with action attached in iPhone

I want to show multiple image in each cell of TableView which is coming from server and I dont know the exact number of images in each table cell.Also when user clicks on any of the images then it will zoom in another view controller.
My question is how to design such table with dynamic height and how to know which images is tapped for zooming.Thanks
i have designed same table as you are currently working on.
For that i have taken UIScrollview in each tableview cell so images that are coming from server will display in scrollview.
And to display images inside the scrollview, i have taken UIButton so i can identify which image is pressed.
This is basic idea what i am doing.
Enjoy!
Try a making a custom Tableview cell with buttons. you can put your images in the button and onclick you can get the sender tag and display the respective image in another view controller
There are many tutorials available on the internet on making a grid view in ios check them out.
Here is a link of one to get you started
Hope this helps.
http://xebee.xebia.in/2011/04/14/building-editable-gridview-for-iphone-apps/
Here we go: you need a custom cell to hold the photos array.
You need a custom UIImageView to track the touch. For this you have 2 options: you add a button on top, or you use -touchesBegan (see below).
Now, when you tap a picture, it will tell it's parent (the cell) which photo was pressed.
The cell will forward the information to the RootViewController (the class with the UITableView) also adding itself to the information.
Classes needed:
RootViewController (not implemented here)
Cell
CustomImageView
//Cell.h
import UIKit/UIKit.h
#class RootViewController;
#class CustomImageView;
#interface Cell : UITableViewCell
{
RootViewController *parent;
IBOutlet UIView *baseView; //I use this instead of content view; is more ..mutable
NSMutableArray *photosArray;
double cellHeight;
}
#property (nonatomic, assign) RootViewController *parent;
#property (nonatomic, retain) UIView *baseView;
#property (nonatomic, retain) NSMutableArray *photosArray;
#property double cellHeight;
(void) didClickPhoto: (CustomImageView*) image;
#end
//Cell.m
import "Cell.h"
#implementation Cell
#synthesize baseView, photosArray, cellHeight, parent;
- (void) didClickPhoto: (CustomImageView*) image
{
unsigned indexOfSelectedPhoto = [photosArray indexOfObject:image];
//this will allow you to reffere the pressed image;
[parent didClickPhotoAtIndex: indexOfSelectedPhoto inCell: self];
//you will inplement this function in RootViewController
}
#end
CustomImageView.h
#import <UIKit/UIKit.h>
#import "Cell.h"
#interface CustomImageView : UIImageView {
Cell *parent;
}
#property (nonatomic, assign) Cell *parent;
#end
CustomImageView.m
#import "CustomImageView.h"
#implementation CustomImageView
#synthesize parent;
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[parent didClickPhoto:self];
}
#end
And that would be the longest answer I ever wrote!!

How to show UIKeyboard upon tapping a button?

I want to show the UIKeyboard upon tapping a UIButton and display the text (typed by the user) on a UILabel. Is this possible?
You can create a hidden UITextField, and set it as firstResponder. As you inputing any chars, copy those chars from hidden UITextField to UILabel.
Why not just replace the label with an appropriately-styled (visible) UITextField? Then swap it back when the user taps the button again (or a Done button somewhere).
This is sorta the opposite of what you want, but this this you can just mess with the code and have it make the keyboard visible on buttonpress. Also in this example, the change is triggered when the user touches the background. You can just set this to whatever button you want instead of the background.
Hiding the Keyboard when the User Taps the Background
#import <UIKit/UIKit.h>
#interface hideKeyboardViewController : UIViewController {
UITextField *textField;
}
#property (nonatomic, retain) IBOutlet UITextField *textField;
- (IBAction)textFieldReturn:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
#end
Select the hideKeyboardViewController.m file and implement the action by calling the resignFirstResponder method of our textField object:
#import "hideKeyboardViewController.h"
#implementation hideKeyboardViewController
#synthesize textField;
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)backgroundTouched:(id)sender
{
[textField resignFirstResponder];
}
.
.
#end
More on this subject here

How to send text from text field to another view controller

I'm making an app that behaves something like the default Messages.app in iPhone where a user composes a text message in a UITextField and upon tapping the Send button, the value of the UITextField in ComposeViewController will be transferred to the table cell's UILabel in a custom cell in MasterViewController and also to the DetailViewController where another UILabel will get the text value from the UITextField. The DetailViewController is the ViewController loaded when the user taps the cells from the UITableViewCells.
I actually read related articles below but it doesn't work on my end.
How to send the text from textfield to another class?
How to see text from one text field in another text field?
How to send text field value to another class
Can you please guide me on how to properly implement this? I know it's easy. I just don't know why it's not working. ComposeVC is a ModalViewController while DetailVC is the view that loads when the user taps the cell in the MasterVC's table.
Thanks a lot!
Below is my code for ComposeVC.h:
UITextField *messageTextField;
#property (nonatomic, retain) IBOutlet UITextField *messageTextField;
- (IBAction)buttonPressed:(id)sender;
for ComposeVC.m
synthesize messageTextField;
-(IBAction)buttonPressed:(id)sender
{
DetailVC *detailVC = [[DetailVC alloc] init];
detailVC.messageText = messageTextField.text;
}
for DetailVC.h
NSString *messageText;
#property (nonatomic, copy) NSString *messageText;
for DetailVC.m
#synthesize messageText;
- (void)viewLoad
{
testLabel.text = messageText;
}
testLabel is the UILabel inside my DetailVC.
You can simply create property on another viewController. Suppose your textField is on view1 and you want to send it on view2 then:
in view 2
.h file
#interface view2:UIViewController {
NSString *str;
}
#property (nonatomic, retain) NSString *str;
in .m file
#synthesize str;
Now you can send your text from view1 to view 2 like:
objView2.str = txtField.text;
For viewing one textfield's text in another use
secondTextField.text = firstTextField.text;
Hope this helped let me know if you are looking for something different.
take one variable in the app delegate like in appdelegate.h
#interface appdelegate
{
NSString *str1;
}
#property (nonatomic, retain) NSString *str;
In .m file synthesize it.
set the text after editing the textfield(app del.str=textfield.text i.e setting value).And use the same wherever you want.
NSString *str = appdel.str(getting value);
Try this one:
#interface SecondView:UIViewController
{
NSString *stringSecond;
}
#property(nonatomic, retain) NSString *str;
In First View u have to create an reference like this:
#import "SecondView.h"
SecondView *detailViewController = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
detailViewController.stringSecond = #"Some string";

View Objects don't connect to the File's Owner

I am trying to "Connect" my objects in the view to the File's Owner using the interface builder, but the blue line does not 'link' with it. Here is my code:
CalculatorViewController.h:
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"
#interface CalculatorViewController : UIViewController {
IBOutlet UILabel *display;
IBOutlet UIButton *button;
CalculatorBrain *brain;
BOOL userIsInTheMiddleOfTypingANumber;
}
- (IBAction)digitPressed: (UIButton *)sender;
- (IBAction)operationPressed: (UIButton *)sender;
#end
CalculatorViewController.m:
#import "CalculatorViewController.h"
#implementation CalculatorViewController
- (CalculatorBrain *)brain
{
if (!brain) {
brain = [[CalculatorBrain alloc] init];
}
return brain;
}
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [[sender titleLabel] text];
if (userIsInTheMiddleOfTypingANumber) {
[display setText: [[display text] stringByAppendingString:digit]];
} else {
[display setText:digit];
userIsInTheMiddleOfTypingANumber = YES;
}
}
- (IBAction)operationPressed: (UIButton *)sender
{
if (userIsInTheMiddleOfTypingANumber) {
[[self brain] setOperand: [[display text] doubleValue]];
userIsInTheMiddleOfTypingANumber = NO;
}
NSString *operation = [[sender titleLabel] text];
double result = [[self brain] performOperation:operation];
[display setText: [NSString stringWithFormat: #"%g", result]];
}
#end
Try and check if class of your File's Owner is set to your ViewController Class properly in your XIB.
For checking Go to your XIB
Click on File's Owner and Open the Inspector.
In inspector, go to the last(fourth) tab and check whether you have set your class as <yourViewControllerName>
Hope this helps you.
EDIT:
For better understanding I have added an image of where you need to look for the class.
Also please cross check that you have declared the variables with IBOutlet Prefix in your ViewController's header file
Once you have made sure the File's Owner is set to be CalculatorViewController, make sure that the IBOutlet type in the CalculatorViewController.h file matches the UI component type you are trying to connect it to. Your header defines an IBOutlet for a UILabel, which means only a UILabel can be connected to it.
If the component is of a different type, lets say a UIButton, then you would change your header file to include an IBOutlet like so:
IBOutlet UIButton *button;
Once you have defined the UIButton in your header, switch to IB. Double check that file's owner is set to your viewcontroller class, then add a UIButton to the view. Then you should be able to either:
ctrl+drag from the component to the file's owner
right-click on the file's owner and get a HUD styled popup showing all available IBOutlet's. click and drag from the UIButton one to the UIButton in your view.
If you want the UIButton in IB to trigger one of your defined IBActions, you will make a connection to the action. I usually perform this by right-clicking on the File's Owner, which will show all available IBActions and IBOutlets.
Hope this helps!
Did you set the file owner(in interface builder) to the class CalculatorViewController.
Im no expert on Interface Builder or Objective C, but did you use:
#property (nonatomic, retain) IBOutlet UIButton *yourButton;
and then in the .m file:
#synthesize yourButton;
Im also not sure, but you may have to 'build' it first, then
go into Interface Builder and then it may 'connect' up.