The purpose of this app is to retrieve the data selected on UITableCell and pass that on to a new view controller. All is well until the user selects the cell. The next view controller is loaded but the screen is black.
On that screen i have a UIImageView and UILabel. I have a UINavigationController set up in the storyboard. I am trying to get the image and text selected on the first screen to the respective objects on the next screen. I have retrieved the data from a database and have loaded it to the first screen.
Here is my code on my storedetail.h which is a subclass to a UIViewController,
#property (nonatomic, copy) UIImage *storeImage;
#property (nonatomic, copy) UILabel *storeCode;
Here is my code where i am calling the nextviewcontroller which is ShowStores.m which is a subclass of a UITableView,
header code ,
#import "ShowStores.h"
#import "StoreDetail.h"
code where i load the next view when the user selects the cell,
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
StoreDetail *storedetail = [[StoreDetail alloc] init];
storedetail.storeImage = cell.imageView.image;
storedetail.storeCode = cell.textLabel;
storedetail.storeCode.text = cell.textLabel.text;
[self.navigationController pushViewController:storedetail animated:YES];
}
I have debugged it and the cell.textLabel.text and cell.imageView.image are not nil or null.
The second view loads but is black. I have set up a back button on the second view which takes the user back to the first view and that works fine.
UIImage and UILabel don't conform to NSCopying protocol, that's most probably the reason. And so declaring #property (nonatomic, copy) doesn't do any effect as your intention.
My suggestion is setting retain for them, and at the point where you want to make copies, just initialize and assign the objects to them.
do you have a nib for your second view controller with the storeCode uiLabel in?
or are you creating the storeCode UILabel programmatically in viewDidLoad?
I would remove this line:
storedetail.storeCode = cell.textLabel;
in this line:
storedetail.storeImage = cell.imageView.image;
storeImage is a UIImage when I think you want it to be a UIImageView. (I would remove that line too and create a storeImageView the same way as you create the storeCode uiLabel - either have it in your nib or create it programmatically in viewDidLoad.
EDIT:
create the objects in viewDidLoad as follows:
storeCode = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 120, 16)];
[self addSubview:storeCode];
For the image I would set the name of the image (filename or path) to an NSString property on your second view controller and then in viewWillAppear use the string to create the correct UIImage and then assign that UIImage to your UIImageView.
Related
I have some cells in a table view that when pressed leads to a view controller that has a label that I want to change.
Example: I have cells representing the biggest citys in America, I press Los Angeles and I get sent to the view controller with the label that changes to a number representing LA. If I go back and press New York the label now displays a number representing New York.
I would guess I give the label and cells identifiers and make some kind of if/else if.
Something like this:
"if 'newyorkcell' isPressed setText 'numbercell' = "12345";"
But with real code.
Thanks in advance!
For this you'll have to make a public outlet of your label in your view controllers header file:
#interface YourViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *yourLabel;
#end
Then you can set that labels text via didSelectRow, for this you have to give your detail view controller a storyboard id (in the storyboard) to be able to access it here in code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"VC"];
detailViewController.yourLabel.text = [self.yourArray objectAtIndex:indexPath.row];
[self.navigationController presentViewController:detailViewController animated:YES completion:nil];
}
Instead of using if/elses you could use an array or something where you have all your data in and access it using its index.
i am kind of making app which play audio using table view
After selecting particular row,it play audio on next view,but it does not solve my problem
Since want it to stop after dismissing the view and return to table view
So i want a way to play song on next view in such a way it stops after dismissing the view
or,
if i can access to the particular row(indexpathy.row) on next view
just property synthesize the raw no or int value to the nextview like bellow..
#interface NextViewController : UIViewController{
int rowNo;
}
#property (nonatomic, assign) int rowNo;
and synthesize in .m file like bellow...
#synthesize rowNo;
and when you push to nextview at that time set this property like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NextViewController *objNextViewController = [[NextViewController alloc]initWithNibName:#"NextViewController" bundle:nil];
objNextViewController.rowNo = indexPath.row;
[self.navigationController pushViewController:objNextViewController animated:YES];
[NextViewController release];
}
Simply take a variable in the next view's class and pass in the value of indexpath.row to it when you are selecting the cell. Since this variable has the value of row selected, you can access this value in next view.
I have a UITextField in My CustomCell of UITableview and i am using this UITableview as Subview of Mainview.There is another UITableview Which is also on my MainView but i am using this UITableview as Subclass of my MainView.Below image will also be hlepfull to Understand my problem.
As image show that above UITableview is my SubClass UITableviw and Below one Which is single row UITableview with CustomCell having UITextField. Now i want that when i Click on any Cell of SubClass UITableview my UITextField Text is Clear which i have in CustomCell of UITableview.
Note:- To Make my question more easir i have some code which works for me when i have UITextfield Simply on my MainView (not in CustomCell of UITableview).here is my Code.
In Mainview.h file
#import <UIKit/UIKit.h>
#import "Inputtableview.h"
#interface testingViewController : UIViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITextField *txtfinput;
Inputtableview *inputview;
IBOutlet UITableView *inputtbl;
}
#property (nonatomic, retain) UITextField *txtfinput;
#end
And then in ViewDidload of Mainview.m file
- (void)viewDidLoad {
if (inputview == nil) {
inputview = [[Inputtableview alloc] init];
}
[inputtbl setDataSource:inputview];
[inputtbl setDelegate:inputview];
inputview.view = inputview.tableView;
inputview.myAccessToMaintxtf = txtfinput;
[super viewDidLoad];
}
Now in my Subclass of UITableview.h file
#import <UIKit/UIKit.h>
#interface Inputtableview : UITableViewController<UITableViewDelegate,UITableViewDataSource> {
}
#property (nonatomic, assign) UITextField *myAccessToMaintxtf;
#end
And Finally in My SubClass UITableview.m file
#implementation Inputtableview
#synthesize myAccessToMaintxtf;
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
myAccessToMaintxtf.text=#"";
}
As i mention Earliar the Above Code works Fine when I have TextField in my MainView,But Did't know How to fix it When i Want to Clear the text of UITextField in CustomCell of UITableview.Any help Will be Appriated.Thanx in Advance.
Ok, I can't see how you are connecting "myAccessToMaintxtf"to the textfield in your CustomCell. What I use for getting subviews from my custom cells are "tags". A tag can be used like an identifier for your subviews. So, if you are using a xib you can set the tag in the settings tab of your textfield, and if you are creating the textfield by code you can set the tag like:
myAccessToMaintxtf.tag = 11; //11 is and example number, use the number that you want
Then when you want to access to your textfield use:
theTextFieldToClear = (UITextField*)[theCellWithTheTextFieldToClear viewWithTag:11];
You can find more information about tags in the UIView Class Reference:
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
Good Luck
EDIT 2:
If you are using two tables, and you want to clear a textfield in one table, selecting the other one, i think that you need something to make the link between the indexPath selected in one table and the indexPath for the cell with the textfield. If you are using something like:
table 1, indexpath:0,0 for celcius;
table 1, indexpath:0,1 for farenheit;
table 1, indexpath:0,2 for kelvin;
and
table 2, indexpath:0,0 for celcius;
table 2, indexpath:0,1 for farenheit;
table 2, indexpath:0,2 for kelvin;
You just can use the same indexPath to get the cell with the textfield FROM THE OTHER TABLEVIEW (Inputtableview) like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cellWithTheFieldToClear = [yourInputTableView cellForRowAtIndexPath:indexPath];
theTextFieldToClear = (UITextField*)[theCellWithTheTextFieldToClear viewWithTag:2];
theTextFieldToClear.text = #"";
}
Did you add your myAccessToMaintxtf as subview of the UITableViewCell in dataSource cellForRowAtIndexPath? If you did, you still have to reload the cell using [tableView reloadCellAtIndexPath:...];
Or another suggestion is to subclass the UITableViewCell, and add an Outlet property, connect the outlet to the TextField. Then you can do something like selectedCell.textField.text = #""; in you didSelectRowAtIndexPath.
I am using a navigation based iPhone application, and I defined an IBOutlet propert in the inner view and synthesized it,
The issue is when I want to set the IBOutlet value before pushing the new view controller, the value won't be set. Here is a snippet from the code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MealDetailViewController *mealViewController = [[MealDetailViewController alloc] initWithNibName:#"MealDetailViewController" bundle:nil];
MealsModel *model = (MealsModel *)[_items objectAtIndex:indexPath.row];
NSLog(model.Name);// here it writes the name right as a string
//mealViewController.lblName.text=model.Name;
[[mealViewController lblName]setText:model.Name];
[[mealViewController txtDesc]setText:model.Description];
[self.navigationController pushViewController:mealViewController animated:YES];
[mealViewController release];
}
I didn't face like these issues in the previous versions of Xcode.
It's to do with when your view controller's views are being created.
When you do
[[mealViewController lblName] setText:model.Name];
you haven't loaded it's view yet so lblName will be nil.
Try either
(a) Explicitly asking for the view, triggering the subviews to be created :
[mealViewcontroller view];
[[mealViewController lblName]setText:model.Name];
[[mealViewController txtDesc]setText:model.Description];
or
(b) Letting the navigation controller create the views for you
[self.navigationController pushViewController:mealViewController animated:YES];
[[mealViewController lblName]setText:model.Name];
[[mealViewController txtDesc]setText:model.Description];
or
(c) Store the values in the mealViewController as properties
MealViewController.h
#property (nonatomic, copy) NSString *lblNameString;
#property (nonatomic, copy) NSString *txtDescString;
MealViewcontroller.m
#synthesize txtDescString, lblNameString;
and instead of setting the label directly, set the properties instead.
[mealViewController setLblNameString:model.Name];
[mealViewController setTxtDescString:model.Description];
Then, in your newWillAppear, setting them
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
lblName.text = lblNameString;
txtDesc.text = txtDescString;
}
(c) is the more correct way to do it - (a) and (b) both fail if your view controller's view is unloaded by a low memory warning.
What I would do is pass the *Meal object to the MealDetailViewController. Then use that to set the labels and such in the MealDetailViewController. Once I had a similar problem and found that I was initializing the property, a UILabel in ViewDidLoad of my receiving class so it was overwriting any passed in text.
i am having a tableview in which custom cells are loaded.Custom cell has a button on click of which a pickerview will open which will have options to choose from.
The problem is that modalViewController method is not working, it is giving the following error.
Selector *sel = [[Selector alloc]initWithNibName:#"Selector" bundle:nil];
[self PresentModalViewController:sel animated:YES];
error:property presentModalViewController not found on object of type CustomCell *...and selector is the pickerview controller class...the method is written in ibaction function in customcell.m file
How can v call other view from custom cell?
thanks
First, naming your class "Selector" is a horribly confusing idea. You should use something more descriptive, and something that is not already an obj-c keyword.
As for your problem, I think you should use a delegate to get a reference from your cell view to the controller. In your custom cell view class, do something like:
#property (nonatomic, assign) id delegate;
// implementation
#synthesize delegate = _delegate;
// in your cell... method
[self.delegate presentPicker];
Here, the delegate ivar would point back to your view controller. To set that up, find the place where you alloc your cell, and do
ACell *aCell = [ACell alloc] init];
aCell.delegate = self;