unable to pass data form tableview to viewcontroller in ios - ios

hi I'm new to iOS development. in my application i have fetch datas form outside database and i viewed in tableview so now i have created on detailviewcontroller. to give clear view about the data but its not working.
the code i used.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
detailviewpoliticalViewController *dvc =[[detailviewpoliticalViewController alloc]initWithNibName:#"dvc" bundle:nil];
fieldpolitical * ff =[eventarray objectAtIndex:indexPath.row];
dvc.eve =ff.title;
[self.navigationController pushViewController:dvc animated:YES];
}
this is the declaration and and initialization of the data in .h file
#import <UIKit/UIKit.h>
#interface detailviewpoliticalViewController : UIViewController
#property (nonatomic,strong) NSString *eve;
#property (strong, nonatomic) IBOutlet UILabel *event;
#end
in .m file in viewload
event.text = self.eve;

I think ff.title is not getting any value, because rest of the things you are doing is fine. Please try to print ff.title before passing it to next view.

First check dvc is your nib(xib) name.
Make exact nib name i thing your nib name should be detailviewpoliticalViewController. i checked from starting if your xib name is correct then tell me i will check next next lines
detailviewpoliticalViewController *dvc =[[detailviewpoliticalViewController alloc]initWithNibName:#"dvc" bundle:nil];

Related

iOS Code Not Running When Method Called From Other Class

I have a custom UITableViewCell xib that has two buttons on it. An edit button, designed to pass the info for that cell into a new view controller is the one I am currently having issues with. I create a property for the TableViewController in my custom cell, and hook up the button to this code:
-(IBAction) editItem {
self.itemsList = [[TheItemsList alloc] init];
[self.itemsList editItem];
NSLog(#"EDIT");
}
Inside TheItemsList I have this code:
-(void)editItem {
NSLog(#"EDITAGAIN");
EditItem *detailViewController = [[EditItem alloc] initWithNibName:#"EditItem" bundle:nil];
detailViewController.selectedCountry = self.selectedCountry;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
detailViewController.therow = indexPath.row;
//Pass the selected object to the new view controller.
// Push the view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
The console prints my NSLog for each method, but the view does not push. This code was previously used in the didSelectForIndexPath part of the table view, so I know that the view controller it is pushing is fine, I just can't figure out what is going on here.
UPDATE:
I have it SOMEWHAT working, emphasis on the somewhat.
ItemCell.h
#import <UIKit/UIKit.h>
#import "TheItemsList.h"
#class TheItemsList;
#class ItemCell;
#protocol ItemCellDelegate <NSObject>
#required
-(void)editTheItem:(ItemCell *)theCell;
#end
#interface ItemCell : UITableViewCell
#property (assign) id <ItemCellDelegate> delegate;
#property (nonatomic, retain) IBOutlet UILabel *itemName;
#property (nonatomic, retain) IBOutlet UILabel *itemPrice;
#property (nonatomic, retain) IBOutlet UILabel *itemAisle;
#property (nonatomic, retain) IBOutlet UIImageView *thumbnail;
#property (nonatomic, retain) TheItemsList *itemsList;
#end
.m
#import "ItemCell.h"
#import "EditItem.h"
#implementation ItemCell
#synthesize itemName = _itemName;
#synthesize itemAisle = _itemAisle;
#synthesize itemPrice = _itemPrice;
#synthesize thumbnail = _thumbnail;
#synthesize itemsList, delegate;
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
-(IBAction) editItem {
[self.delegate editTheItem:self];
}
#end
Now, in the TableView
.h
#import <UIKit/UIKit.h>
#import "ItemCell.h"
#class ItemCell;
#protocol ItemCellDelegate;
#interface TheItemsList : UITableViewController <ItemCellDelegate>
.m
-(void)editTheItem:(ItemCell *)theCell {
NSLog(#"EDITAGAIN");
EditItem *detailViewController = [[EditItem alloc] initWithNibName:#"EditItem" bundle:nil];
detailViewController.selectedCountry = self.selectedCountry;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
detailViewController.therow = indexPath.row;
//Pass the selected object to the new view controller.
// Push the view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
What I'm running into is two problems.
1, the first item in the tableview doesn't do anything when the button is pressed.
2, the other rows will run code, but it only does it for the first row's data.
You should use the delegate pattern inside your UITableViewCell to delegate the button press back to the UIViewController which contains the UITableView using the cell.
In cellForRowAt:IndexPath you then retrieve the cell and set its delegate like cell.delegate = self. In the view controller you implement the delegate and handle the action.
In the cell you setup a delegate variable (var delegate: MyCellDelegate?) and inside the editItem function you make a call to the delegate like self.delegate?.editItem()
Your issue is basically because of this line
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
On clicking the button on the cell, it does not select the row necessarily. The button takes the touch. A way to solve this would be to find the NSIndexPath of the cell in which the button is present and then do your operations with it. A way to do is to use the cell reference that is being supplied from the editAction delegate
-(void)editTheItem:(ItemCell *)theCell
Replace [self.tableView indexPathForSelectedRow] with [self.tableView indexPathForCell:theCell]

UICollectionView:didSelectItemAtIndexPath: How to populate new view with data based on selection?

Here's the deal:
I have a UICollectionView which populates itself from an array of objects. I want to be able to tap one of the views representing an object in my UICollection view and be taken to a new view displaying details of the object I selected in the UICollectionView (like the selected object's ID for example).
Unfortunately, whatever I try to set is always null!
Here is the object:
#interface obj : NSObject
#property (nonatomic, strong) NSString *objId;
...
#property (nonatomic, strong) NSString *imageURL;
#end
Here is the main view controller:
#interface ViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
#property (weak, nonatomic) IBOutlet UICollectionView *badgesCollectionView;
#property (strong, nonatomic) NSArray *objArray;
...
#end
Here is the detail view:
#interface DetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *objId;
#end
Here is where I'm trying to populate the detail view (in my main ViewController.m)
// Implement action for object selection
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailView = [[DetailViewController alloc] init];
detailView.objIdLabel.text = [[self.objArray objectAtIndex:indexPath.row] objId];
[self.navigationController pushViewController:detailView animated:YES];
NSLog(#"Object Selection: Report");
NSLog(#"Label Text : %#", detailView.objId.text);
NSLog(#"Id of object : %#", [[self.objArray objectAtIndex:indexPath.row] objId]);
}
When I select an object in UICollectionView, I get a transition to a detailView without anything populated. Here is what's logged:
Object Selection: Report
Label Text : (null)
Id of object : f0f47920-f449-4fd0-a74e-804546905437
Any insight into this would be fantastic. Thanks!
The output of the UILabel's text field is null because the label property is not actually linked to anything until the view is loaded. In this case, it wont happen until after you push the view. In order to get around this, you can just have a property on your DetailViewController that holds the string until the view appears.
Then in viewDidLoad or viewWillAppear in DetailViewController, you can set the text on objIdLabel to the property that contains the string
The problem can come from the DetailViewController implementation.
DetailViewController *detailView = [[DetailViewController alloc] init];
only init the Controller
but you declare
#interface DetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *objId;
#end
wich means you must use a xib.
So you first have to use
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailViewXIBName" bundle:nil];
then try to affect the value to objIdLabel once the controller is pushed:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailViewXIBName" bundle:nil];
[self.navigationController pushViewController:detailView animated:YES];
detailView.objIdLabel.text = [[self.objArray objectAtIndex:indexPath.row] objId];
NSLog(#"Object Selection: Report");
NSLog(#"Label Text : %#", detailView.objId.text);
NSLog(#"Id of object : %#", [[self.objArray objectAtIndex:indexPath.row] objId]);
}
You have to affect the text after [super viewDidLoad]; is called.
These are just assumptions as we only have limited information
It may push a blank view controller because you are not strongly holding onto the detailView.
Try adding:
#property (nonatomic, strong) DetailViewController *detailView;
to the main ViewController, then in the didSelectItemAtIndexPath method:
// Implement action for object selection
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
self.detailView = [[DetailViewController alloc] init];
self.detailView.objIdLabel.text = [[self.objArray objectAtIndex:indexPath.row] objId];
[self.navigationController pushViewController:self.detailView animated:YES];
}
It turns out this comes down to a xib's lifecycle. It is true, as many of you suggested, that the detailView is not actually linked with all of the items associated with it (like the objId UILabel) until it's loaded.
I ended up fixing this by creating a new singleton model object to hold onto the selected object. I was then able to set the selected object on the singleton from my main view controller and call it from the detailView to set my objId on ViewDidLoad.

xCode Detail View is Empty

I am creating an iPad application (iOS6.1) which has a master detail view concept. First view is a table view has list of items that are been loaded from Plist, when each row gets selected the second table view gets loaded with another Plist. theirs is my Detail view which has to display an UIView with a UILabel ans an UIImage. I am using didSelectRowAtIndexPath method . The first two table Views are been displayed properly and loads the row and display corresponding View but the last detail view which is supposed to display the UILabel and an image is empty, can any one help me to solve this problem
My Code for the didSelectRowAtIndexPath method is
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TaskDetailViewController *newTaskDetailViewController = [[TaskDetailViewController alloc] init];
// pass the row to newDetailViewController
if (weekNumber == 0)
{
newTaskDetailViewController.taskdescription = [weeklist1 objectAtIndex:indexPath.row];
}
if (weekNumber == 1)
{
newTaskDetailViewController.taskdescription = [weeklist2 objectAtIndex:indexPath.row];
}
if (weekNumber == 2)
{
newTaskDetailViewController.taskdescription = [weeklist3 objectAtIndex:indexPath.row];
}
// ...... repeated for 39 times because of the list
newTaskDetailViewController.taskNumber = indexPath.row;
[self.navigationController pushViewController:newTaskDetailViewController animated:YES];
}
DetailView header
#import <UIKit/UIKit.h>
#interface TaskDetailViewController : UIViewController
#property int taskNumber;
#property(strong , nonatomic) NSString *taskdescription;
#property (nonatomic , strong) NSMutableDictionary * tasks;
#property (strong, nonatomic) IBOutlet UIImageView *questionImage;
#property (strong, nonatomic) IBOutlet UILabel *displayText;
#end
Implemetation file has
#implementation TaskDetailViewController
#synthesize taskNumber;
#synthesize taskdescription;
#synthesize tasks;
#synthesize displayText;
#synthesize questionImage;
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = taskdescription;
NSLog(#"%#", taskdescription);
}
Your problem is using alloc init to create an instance of TaskDetailViewController. You've created that controller in the storyboard so you should instantiate it from the storyboard using an identifier that you give it (DetailViewController in my example):
TaskDetailViewController *newTaskDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];

how to pass data from tableview to another tableview cell?

I am using static UITableView to show profile of a customer.
It has SelectCountryCell to select country which, when clicked, opens new UITableView with country list.
When I select country from Country TableView, It should display in previous TableView's SelectCountryCell.
How do I do this?
You should bind the static cells to outlets in your UITableViewController and put the profile syncing methods to the - viewWillAppear method.
When the user changes the country in the Country list the profile updates. After that when the user moves back to the UITableViewController instance with static content the profile data will be automatically updated.
You can define a delegate in your CityTableView and then define a method in this delegate.
You should realize this method in your CountryTableView.
Then you may get what you want.
I only offer you a thought. You should find the detail by yourself.
MasterViewController.h
#import "DetailViewController.h"
#interface MasterViewController : UITableViewController <DetailProtocol> // Note this.
#property (strong, nonatomic) DetailViewController *detailViewController;
#property (strong, nonatomic, readwrite) NSString *selectedCountry;
#end
MasterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
self.detailViewController.delegate = self; // THIS IS IMPORTANT...
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
// Note this -- It's a delegate method implementation
- (void)setCountry:(NSString *)country
{
self.selectedCountry = country;
}
DetailViewController.h
#protocol DetailProtocol <NSObject>
-(void)setCountry:(NSString *)country;
#end
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#property (unsafe_unretained) id <DetailProtocol> delegate; // Note this
#end
DetailViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.delegate setCountry:[countries objectAtIndex:indexPath.row]]; // Note this
[self.navigationController popViewControllerAnimated:YES];
}

How to pass latitude value to next view in iphone?

I have got response from services and I have got latitude value and I copied into an array.
Now I want to pass the array when I click the next views row.
I passed places earlier. In second view UITableViewController is there if I click the particular row then it should be printed the rows text value and particular text's latitude value.....?
Is this possible...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *myText=[places objectAtIndex:indexPath.row];
NSLog(#"his is selected lattidude value:%#",myText);}
Here I am printing the row's text with this I need to print the latitude value of each one....please help me.
There are lot many way to pass data from one view to other view.
Best for you is like:
Just create one variable in your second view controller.
When you push navigation from current page just before assign your value to that variable.
now you can easily use that value in your second view.
in your SecondViewController in .h file just
#interface SecondViewController : UIViewController{
NSString *strLatLong;
}
#property (nonatomic, retain) NSString *strLatLong;
#end
and in .m file just synthesize it like bellow..
#synthesize *strLatLong
and in your FirstViewController class just push with bellow code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *myText=[places objectAtIndex:indexPath.row];
NSLog(#"his is selected lattidude value:%#",myText);
SecondViewController *objSecondView = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
objSecondView.strLatLong = myText;
[objSecondView.strLatLong retain];
[self.navigationController pushViewController:objSecondView animated:YES];
[objSecondView release];
}
i hope this help you...
Create #property in second viewcontroller class .h file
#property (nonatomic ) NSString *StrName;
#synthesize in second viewcontroller class .m file
#synthesize StrName;
after that in your current viewcontroller class
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//NSString *myText=[places objectAtIndex:indexPath.row];
secondviewcontroller *ss=[[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
ss.StrName=[places objectAtIndex:indexPath.row];
[self presentModalViewController:ss animated:YES];
}
you must create a variable in second class and define his property and on first class you use this property to send one value to next class you sed value like this
in first class you create a variable with property like this
#property(nonatomic ,strong) NSString *str;
and in first class when you create the object of second class then you send the value like this
SecondClass *secondClass=[[SecondClass alloc]init];
secondClass.str=#"value is here";
in this method you send value to next class
i hope you understand ;-)

Resources