Static cell with TextField - connect class property to viewcontroller - ios

I want to be able to have a TextField in my static cells so I'm using the StringInputTableViewCell class from this demo: https://github.com/wannabegeek/PickerTableViewCell/tree/master/PickerCellDemo
I'm using storyboards and in my storyboard I got 2 cells that use the StringInputTableViewCell class. As you can see there is a UITextField property
#property (nonatomic, strong) UITextField *textField;
in StringInputTableViewCell.h. On storyboards in MyViewController I can connect the label, etc with a property but I cannot connect the TextField since its added in code and the TextField isn't on my storyboard. So my question is how do I get that property in MyViewController class?

You can make your "StringInputTableViewCell" a property within your "MyViewController", or if you're trying to populate it during "cellForRowAtIndexPath", you can get get access to it through an identifier or a tag.
Once you have a pointer to the "StringInputTableViewCell" object that's being displayed in your table, you can then access the "textField" property of it in your code directly.
Something like:
StringInputTableViewCell * stringInputCell = (StringInputTableViewCell *) cell;
if(stringInputCell)
{
stringInputCell.textField.text = #"Surprise, I can put text in here!";
}

Related

Index Path of Custom UITableViewCell

I have a custom UITableViewCell which contains two UIButtons (An upVote button and a downVote button), and a label that is meant to count the number of votes. I am using the Parse framework for my backend.
I cannot figure out how to associate the value of a particular label with the custom UITableViewCell that contains the label so that I can then save it to the Parse backend. How can I reference the indexPath of the cell that contains the button using Swift and then associate it with the label so that I can then save it to Parse?
As always, if you feel there is a better way, please share.
.
Everything can happen in your custom UITableViewCell. The key is to store the parse object as a property of the UITableViewCell in cellForRowAtIndexPath: so you never need to worry about looking up the indexPath. Hook your two UIButtons up and when a button is tapped: update the vote count on the parse object, update the label, save the parse object. Something like this should give you the idea:
#interface CustomTableViewCell
#property (nonatomic, strong) MyParseData *parseObject;
#property (nonatomic, strong) UILabel *voteCountLabel;
#end
#implementation CustomTableViewCell
- (IBAction)upVoteButton:(id)sender {
self.parseObject.voteCount++;
[self updateVote];
}
- (IBAction)downVoteButton:(id)sender {
self.parseObject.voteCount--;
[self updateVote];
}
- (void)updateVote {
self.voteCountLabel.text = [self.parseObject.voteCount description];
[self.parseObject saveInBackground];
}
#end
I think you created a custom cell to do this. So you have to create two methods in the custom cell class that will handle the touch up inside to its delegate (the tableview controller, for example). When delegate is called, it has a reference to the cell touched and you can go on.

How to bind the control on the form with variable

I am just start studying iOS developing watching Stanford iOS course, but it looks like I have already missing something.
I have a form with UILabel and UIButton. When an user press the button the title of the button must be added to the text of label.
Here is my current CalculatorViewController.h:
#import <UIKit/UIKit.h>
#interface CalculatorViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *display;
#end
and here is a CalculatorViewController.m:
#import "CalculatorViewController.h"
#implementation CalculatorViewController
#synthesize display = _display;
- (IBAction)digitPressed:(UIButton *)sender {
NSString *digit = [sender currentTitle];
UILabel *myDisplay = self.display;
myDisplay.text = [myDisplay.text stringByAppendingString:digit];
}
#end
The problem is that self.display (and myDisplay) variables have a nil value. Looks like I need to do something to link my variable with control on the form. What ?
Thanks in advance.
You need to link the control, the UILabel in Interface Builder to the variable in your CalculatorViewController class.
It is very likely that the file's owner (talking about the Xib file) is your CalculatorViewController, so you need to Control+drag the file's owner (or the object representing your VC) to the control and you will be shown a menu with the possible IBOutlet variables declared in your class, so you select the one you want to represent the control.
You can check if this link is properly set in two ways: (1) Select the UILabel in Interface Builder and see if there's a connection to the variable in the Connections Inspector, or (2) In your code you'll see a bullet near the IBOutlet declaration, if the bullet is hollow the connections is not set, if the bullet is filled the connection is set.
There is no need of this line
UILabel *myDisplay = self.display;
You have already declared your label in your interface file
- (IBAction)digitPressed:(UIButton *)sender
{
NSMutableString *string = [myDisplay.text stringByAppendingString:sender.titleLabel.text];
myDisplay.text = string;
}

How to get a reference to Table View Controller from Custom Table View Cell class

I have custom table view cell and have made a custom class. How can I get reference to the table view controller from the cell?
I have this custom tableview cell class. In this I have a button which is connected with a tap event. On tap I get the event fine but I am looking to get a hold on the table view controller so I can show the action sheet on top of the table view.
#interface MTPurchasedCartItemCell : UITableViewCell
- (IBAction)onShareTap:(id)sender;
#end
The way I would do this would be to use a block event handler. In your MTPurchasedCartItemCell class add a property in the header file like so:
#property (nonatomic, copy) void (^tapHandler)(id sender);
And in the implementation file you can do this:
- (IBAction)onShareTap:(id)sender {
if (self.tapHandler) {
tapHandler(sender);
}
}
And finally in your controller class, do something like this in your cellForRowAtIndexPath::
...
cell.tapHandler = ^(id sender) {
// do something
}
...
You might also consider adding a public #property for the button to the custom cell.
To do that, add the following line to the header file of your custom cell class:
#property (weak, nonatomic) IBOutlet UIButton *myButton;
That's if you're creating the button in Interface Builder. If you're creating the button in code, you'd add this line instead:
#property (nonatomic) UIButton *myButton;
Then, when initializing or configuring the cell from your table view controller, you now have a reference to myButton and can add an action to it with your table view controller as the target.
Then, in the action method, you can get the cell from the button. The answer to Objective-C: How to generate one unique NSInteger from two NSIntegers? explains how.
I've done something very similar that gets the table reference from a custom cell using a button event.
Code:
-(IBAction)onShareTap:(UIButton *)sender { // i've used a button as input..
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)senderButton.superview.superview.superview;
UITableView* table = (UITableView *)[buttonCell superview];
NSLog(#"Table: %#", table);
}

How can I pass the ApplicationViewController to a different class?

I have a single view application that has a subview. That subview contains a form that includes e-mail textfield and password textfield. I need to set the delegates to those textfields to the "global" this.
Everything is written code, nothing on the storyboard.
How can I pass my ApplicationViewController to the textfields?
Add a #property for your viewController and pass that to your UIView class.
In your UIViewClass.h
#property (nonatomic, strong) UIViewController *yourViewController;
Then pass the current viewController to your UIView when you create it.
UIView *yourView = [UIView new];
yourView.yourViewController = self;
Then you can access your viewController directly from in your UIView class.
yourTextField.delegate = _yourViewController;

How to change a label on one UIView from another UIView

Within my iPhone app I have 2 UIControls a ViewController.h and a selectionScreen.h.
The selectionScreen is set as so
#interface selectionScreen : ViewController{
I am trying to change a label that is placed in the main ViewController, from the selectionScreen
So far I have this within the SelectionScreen.m (personTotal1.text is the label in the other UIcontrol)
- (IBAction)Change:(id)sender{
int x=123;
NSString *y =[NSString stringWithFormat:#"%i",x];
personTotal1.text=y;
NSLog(#"%#",personTotal1.text);
}
When I NSLog to check if the value has changed it returns null, how can I make it so interactions(such as a button press) within the Selectionscreen change label text in another screen.
In your View controller:
Did you set your personTotal1 ivar as a property? Just setting the ivar is not enough:
UILabel *personTotal1;
You need to set the property and synthesize the getters and setters like so:
#property(nonatomic, retain) UILabel *personTotal1;
If you are using ARC then it would be like so:
#property(nonatomic, strong) UILabel *personTotal1;
Then be sure to synthesize the label. Now import the ViewController.h into the SelectionScreen file. From there you can access the UILabel properties. Hope this helps, if not let me know and I can clarify.

Resources