unbalanced transition ios - ios

I have this problem that when my code executes and then I click on item on table, it takes me to detail view. Things display properly but in the console there is this message:
2013-03-27 13:09:30.616 LinkTest[3605:c07] Unbalanced calls to
begin/end appearance transitions for
.
I know that this is reported here often and looked through the answers, but nothing seems to work. Maybe I got something wrong. Here is the code for tableviewcontroller:
#import "EventListingTableViewController.h"
#import "EventListingDetailViewController.h"
#interface EventListingTableViewController ()
#end
#implementation EventListingTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void)fetchEventDetails
{
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://xsysdevelopment.com/ios/read.php"]];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
self.eventsTitles = [[NSMutableArray alloc] init];
self.eventsCity = [[NSMutableArray alloc] init];
for(id object in dict){
[self.eventsTitles addObject:object[#"title"]];
[self.eventsCity addObject:object[#"city"]];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchEventDetails];
// Uncomment the following line to preserve selection between presentations.
//self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.eventsTitles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
int row = [indexPath row];
cell.textLabel.text = self.eventsTitles[row];
cell.detailTextLabel.text = self.eventsCity[row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
EventListingDetailViewController *detailViewController = [[EventListingDetailViewController alloc] initWithNibName:#"EventListingDetailViewController" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"eventDetailSeague"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
EventListingDetailViewController *detailViewController = [segue destinationViewController];
int row = [indexPath row];
detailViewController.eventDetails = #[self.eventsTitles[row], self.eventsCity[row]];
}
}
I will really appreciate it if someone tells me where I am going wrong. I am new to iOS so only helpful criticism would be useful.
regards

If you are using segues via storyboards for the UITableView then you don't need the code in didSelectRowAtIndexPath:. I believe you a firing a push twice due to this.

Related

Tableview data won't load when unsegmented control is changed

//The tableview is not changing. I have been at this for days. It seems so simple. Thank you for your help
#import "StopsTableViewController.h"
static NSString *MyIdentifier = #"RouteListing";
#interface StopsTableViewController ()
#property (strong, nonatomic) NSArray *TESTARRAY;
#end
#implementation StopsTableViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//Set the tab bar item's title
self.title = #"Stops";
}
self.stopList = [[API sharedAPI] fetchStopListing];
self.TESTARRAY = #[#"Josh", #"Kyle", #"Nate"];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MyIdentifier];
// Adds Segmented Control
[self segmentedView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) segmentedView {
NSArray *segmentedMenu = [NSArray arrayWithObjects:#"All", #"Near Me", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedMenu];
[segmentedControl addTarget:self
action:#selector(valueChanged:)
forControlEvents:UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
self.navigationItem.titleView = segmentedControl;
}
pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([self.segmentedControl selectedSegmentIndex] == 0) {
return [self.stopList count];
}
else {
return [self.TESTARRAY count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (_segmentedControl.selectedSegmentIndex == 0) {
cell.textLabel.text = [[self.stopList objectAtIndex:indexPath.row] objectForKey:#"stoptitle"];
cell.detailTextLabel.text = [NSString stringWithFormat:#"Stop %#", [[self.stopList objectAtIndex:indexPath.row] objectForKey:#"stopnumber"]];
}
else {
cell.textLabel.text = self.TESTARRAY[indexPath.row];
}
return cell;
}
pragma mark - Table view delegate
// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here, for example:
// Create the next view controller.
StopInfoViewController *detailViewController = [[StopInfoViewController alloc] initWithNibName:#"StopInfoViewController" bundle:nil];
// Pass the selected object to the new view controller.
detailViewController.stopInfo = [[self.stopList objectAtIndex:indexPath.row] objectForKey:#"stopnumber"];
detailViewController.stopName = [[self.stopList objectAtIndex:indexPath.row] objectForKey:#"stoptitle"];
// Push the view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
//Function for segmentedView
-(void) valueChanged:(UISegmentedControl *)sender {
[self.tableView reloadData];
NSLog(#"I'm getting called segment number is: %ld", (long)sender.selectedSegmentIndex);
}
#end
Check your datasource array before calling the table reload method and make sure that the array contains new values corresponding to the segment that you have selected.
One thing I always run into when struggle up a table view is connecting the table with the interface using interface builder. Because it sounds like you're getting the data loaded, when the view first loads. But when you call [self.tableView reloadData] without having the connection made to the table, nothing will happen.
I ways forget that super simple step. Hope this helped
Did you set the tableview delegates?
Try putting this in your viewDidLoad function.
[tableView setDelegate:self];
[tableView setDataSource:self];
If you want a fuller explanation, check here:
how to set a tableview delegate
That post also explains how to ensure your class subscribes to the UITableViewDelegate and UITableViewDataSource protocols
Hope that helps.

Segue to UITableView depending on Row Selection

I have a UITableView which contains an array of 3 items. Upon user selection, I need each of these three items to segue to a different UITableView. For example:
First Scene is the table which contains the list: Weather, Intelligence, Fuels Status
Second scene would be:
For when a user selects weather, segue to a table which contains the array "daily maps, fire potential, etc."
For when a user selects intelligence, segue to a table which contains the array "current active fires, new initial attacks, etc."
and so forth.
I have been told I could use different UITableViewCells for each prototype cell but I'm sure there is an easier way that I am simply not grasping. All current segues were done in storyboard. Could someone please elaborate on how I should be segueing from scene to scene (at a beginner-intermediate level of understanding)?
Root
#import "PSMenu_TableViewController.h"
#import "WXMenu_TableViewController.h"
#import "PSTableViewCell.h"
#interface PSMenu_TableViewController ()
#end
#implementation PSMenu_TableViewController
#synthesize PSMenuImage = _PSMenuImage;
#synthesize PSMenuText = _PSMenuText;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.PSMenuText = [[NSArray alloc]
initWithObjects:#"Weather",
#"Intelligence",
#"Fuels Status",
nil];
self.PSMenuImage = [[NSArray alloc]
initWithObjects:#"RMACC_114x114.png",
#"RMACC_114x114.png",
#"RMACC_114x114.png",
nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_PSMenuText count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"rootTableCell";
PSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == 0) {
cell = [[PSTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.rootLabel.text = [self.PSMenuText
objectAtIndex: [indexPath row]];
UIImage *rootPhoto = [UIImage imageNamed:
[self.PSMenuImage objectAtIndex: [indexPath row]]];
cell.rootImage.image = rootPhoto;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Use indexPath to retrieve info that needs to be passed along to next view controller
[self performSegueWithIdentifier:#"getWeather" sender:self];
}
#end
One of the .m files I am trying to segue to:
#import "WXMenu_TableViewController.h"
#import "DWO_TableViewController.h"
#interface WXMenu_TableViewController ()
#end
#implementation WXMenu_TableViewController
{
//Define array for weather products list
NSArray *allwx;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Define the weather products list
allwx = [NSArray arrayWithObjects:#"Daily Weather", #"Fire Potential", #"Multi-Media Briefing", #"Sig. Fire Potential",#"Seasonal Outlook", #"Fire Season Broadcast", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Set table size to one section.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//When Weather is clicked on previous VC, the allwx list is displayed
if ([_weathertable isEqualToString:#"Weather"]) {
return [allwx count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *pstableID = #"MainCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pstableID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pstableID];
}
if ([_weathertable isEqualToString:#"Weather"]) {
cell.textLabel.text = [allwx objectAtIndex:indexPath.row];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:#"showDWODetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DWO_TableViewController *destViewController = segue.destinationViewController;
destViewController.dailywxtable = [allwx objectAtIndex:indexPath.row];
destViewController.title = destViewController.dailywxtable;
}
}
#end
You can segue to a single UITableViewController here and make it display respective contents according to the category chosen. This might help you.
In your Destination View Controller, you can declare a variable and use it in your prepareForSegue method as shown to identify what type of content do you want to see on your destination view controller.
destController.varName = #"setYourTypeHere";
Now you can also set the variable value here based on the Row you are clicking in your tableview.
And in your Destination View controller, you can check this value using switch or if...else cases and bind the appropriate data as per your requirement.
In storyboard you wire up each destination not to the prototype tableview cell, but to the view controller itself (little yellow box on left side of the title bar of the view controller in the storyboard).
Name each segue with a meaningful name, and then in your "didSelect" delegate method you can perform a segue using:
[self performSegueWithIdentifier:#"myNamedSegue" sender:self];

Creating a view controller

I'm creating an iphone app using storyboard. I'm basically new on objective c and Xcode.
I have a list of categories, every time I click on a category it should open a tableView, so I can add an item in that category. But instead of getting a different tableView for each category, its the same table for all the categories and the added items are copied.
How can I create a new table for each label?
Thanks in advance!
Here's what I have for adding a category
#interface ListViewController ()
#end
#implementation ListViewController{
NSMutableArray *items;
}
#synthesize lists;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
items = [NSMutableArray arrayWithCapacity:20];
List *item = [[List alloc] init];
item.title = #"Grocery List";
[items addObject:item];
item = [[List alloc]init];
item.title = #"Project List";
[items addObject:item];
item = [[List alloc] init];
item.title = #"Events List";
[items addObject:item];
self.lists = items;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.lists count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell...
/*UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListsCell"];
List *list = [self.lists objectAtIndex:indexPath.row];
cell.textLabel.text = list.title;*/
ListCell *cell = (ListCell *)[tableView dequeueReusableCellWithIdentifier:#"ListsCell"];
List *list = [self.lists objectAtIndex:indexPath.row];
cell.titleLabel.text = list.title;
return cell;
}
//Add new list, new row will be added on the bottom and its data source must always be sync
-(void)addViewControllerSave:(AddViewController *)controller addList:(List *)list{
[self.lists addObject:list];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.lists count] - 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
if(editingStyle == UITableViewCellEditingStyleDelete){
[self.lists removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
/* else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}*/
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
*/
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if([segue.identifier isEqualToString:#"AddList"]){
UINavigationController *navigationController = segue.destinationViewController;
AddViewController *addViewController = [[navigationController viewControllers] objectAtIndex:0];
addViewController.delegate = self;
}
else if([segue.identifier isEqualToString:#"ViewItem"]){
UINavigationController *nav = segue.destinationViewController;
ItemViewController *itemViewController = [[nav viewControllers] objectAtIndex:0];
itemViewController.delegate = self;
}
}
#pragma mark - AddViewControllerDelegate
-(void)addViewControllerCancel:(AddViewController *)controller{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)addViewControllerSave:(AddViewController *)controller{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)itemViewControllerBack:(ItemViewController *)controller{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
Here's what I have for adding an item:
#interface ItemViewController ()
#end
#implementation ItemViewController{
NSMutableArray *newItems;
}
#synthesize items;
#synthesize delegate;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
newItems = [NSMutableArray arrayWithCapacity:20];
Item *i = [[Item alloc]init];
i.listItem = #"a";
[newItems addObject:i];
self.items = newItems;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemsCell"];
Item *item = [self.items objectAtIndex:indexPath.row];
cell.textLabel.text = item.listItem;
return cell;
}
-(void)addIteViewControllerSave:(AddItemViewController *)controller addItem:(Item *)item{
[self.items addObject:item];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.items count] -1 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if([segue.identifier isEqualToString:#"AddItem"]){
UINavigationController *navigationController = segue.destinationViewController;
AddItemViewController *addItemViewController = [[navigationController viewControllers]objectAtIndex:0];
addItemViewController.itemDelegate = self;
}
}
#pragma mark - AddItemViewControllerDelegate
-(void)addItemviewControllerCancel:(AddItemViewController *)controller{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)addIteViewControllerSave:(AddItemViewController *)controller{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - ItemViewControllerDelegate
-(IBAction)back:(id)sender{
[self.delegate itemViewControllerBack:self];
}
#end
AddItemViewController
#import "AddItemViewController.h"
#import "Item.h"
#interface AddItemViewController ()
#end
#implementation AddItemViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)cancel:(id)sender{
[self.itemDelegate addItemviewControllerCancel:self];
}
-(IBAction)save:(id)sender{
Item *item = [[Item alloc] init];
item.listItem = self.listItemTextField.text;
[self.itemDelegate addIteViewControllerSave:self addItem:item];
}
#end
Don't create a new table view for each label, instead populate it with different data for each label, you will need to change the object you are storing your data in and call
[tableView reloadData];
If all the added items you may need to clear out your array you are using to store the objects before adding the additional content.
[self.items removeAllObjects];

URL From Plist to Detail View Controller's Web View [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a band application where the app needs to have instrument scales on it. To do this I am trying to have a PList file with the Name of the Scale and a Description. I have researched and all I can find is for passing images, not URL's. I have the plist done. Here is a part of it:
And I also have a table view getting the Name and the Scale Part of the Plist file.
What I can't figure out how to do is, how do I drill down into a Detail View controller and then load the URL for the clicked Table View Cell into a UI Webview, Here is the Code from the .m of the Main Table view:
#import "TableViewController.h"
#import "DetailView.h"
#interface TableViewController ()
#end
#implementation TableViewController
#synthesize content = _content;
-(NSArray *)content
{
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Scales" ofType:#"plist"]];
}
return _content;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"Detail"])
{
// Get reference to the destination view controller
}
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:#"Name"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:#"Scale"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
`
If anybody has any idea how to do this, please help me?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.webLink = [[self.content objectAtIndex:indexPath.row] valueForKey:#"URL"];//webLink is NSStringProperty in DetailViewController.h
[self.navigationController pushViewController:detailViewController animated:YES];
}
Load UIWebView inside viewDidLoad of DetailViewController use XIB
- (void)viewDidLoad
{
[super viewDidLoad];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webLink]]];
}
About how to push to WebViewController, refer sample code:
https://github.com/lequysang/github_zip/blob/master/TestTapOnWeb.zip
With storyboard.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *webLink = [[self.content objectAtIndex:indexPath.row] valueForKey:#"URL"];//webLink is NSStringProperty in DetailViewController.h
[[segue destinationViewController] setWebLink:webLink];
}
}

Load DataController before ViewController iOS

I have 2 Controllers, 1 is to load data from JSON, the other is a simple UITableViewController. My problem is that the view is loaded before the data. How can I make the data load before my table? I'm new to Objective-C OOP :/
the code
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "DealsDataController.h"
#import "Deals.h"
#implementation MasterViewController
- (void)awakeFromNib
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
[super awakeFromNib];
self.dataController = [[DealsDataController alloc] init];
NSLog(#"this is the awake from nib count %i",[self.dataController countOfList]);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"did this count the list %i",[self.dataController countOfList]);
return [self.dataController countOfList];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"DealCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Deals *dealAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
[[cell textLabel] setText:dealAtIndex.name];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
/*
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSDate *object = _objects[indexPath.row];
self.detailViewController.detailItem = object;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
*/
#end
and the Data Controller
#import "DealsDataController.h"
#import "Deals.h"
#implementation DealsDataController
#synthesize masterDealsList = _masterDealsList;
-(id)init {
if (self = [super init]) {
[self initializeDataList];
return self;
}
return nil;
}
-(NSMutableArray *)masterDealsList {
if (!_masterDealsList) {
_masterDealsList = [[NSMutableArray alloc] init];
}
return _masterDealsList;
}
-(void)setMasterDealsList:(NSMutableArray *)newList {
if (_masterDealsList != newList) {
_masterDealsList = newList;
}
}
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
_deals = [json objectForKey:#"deals"]; //2
NSLog(#"deals: %#", _deals);
NSArray *dealName = [_deals valueForKey:#"deal_name"];
NSLog(#"deals Name: %#", dealName);
for (int i = 1;i <= [_deals count]; i++) {
NSDictionary* dict = [_deals objectAtIndex:i-1];
Deals *deal =[[Deals alloc] initWithProdID:1 name:[dict valueForKey:#"deal_name"] description:[dict objectForKey:#"deal_description"] price:10.00 specs:#"specs" terms:#"terms"];
[self addDealWithDeal:deal];
NSLog (#"masterListCount %i ", [self countOfList]);
}
}
-(void)initializeDataList {
//NSDate *today = [NSDate date];
dispatch_async(sweetDealsQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:sweetDealsURL];
[self performSelectorOnMainThread:#selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
-(NSUInteger)countOfList {
return [self.masterDealsList count];
}
-(Deals *)objectInListAtIndex:(NSUInteger)theIndex {
return [self.masterDealsList objectAtIndex:theIndex];
}
-(void)addDealWithDeal:(Deals *)deal {
[self.masterDealsList addObject:deal];
}
#end
In MVC, you'd typically have one view controller per view. So a single view controller would be responsible for everything display on one view. Of course, you can have helper classes, or use sub view controllers (e.g. widgets), or if you're developing for iPad, you might have two view controllers (one for the side menu, one for the main view).
I'd suggest you do the following
Make your DataController a subclass of NSObject instead of UIViewController. From what I understand, it is a data access class, not responsible for UI.
In the viewDidLoad method of your MasterViewController, allocate and initialise a DataController object and trigger its data load method.
Either set a callback so when data is fetched, a method on the MasterViewController is called with the data. Or set your MasterViewController as a delegate for your DataController and when it's done, assign the data to a property of the MasterViewController.
i guess you need to perform Data loading before initialising your TableViewController and as you get response Data load the TableViewController. for that refer this post how to wait until some method complete execution, perform data loading in that method.
Its okay to display a blank tableview with an activity indicator overlay while the data is loading. When the data has finished loading, you can just reload the tableview with the data and remove the activity indicator. [tableview reloaddata] would call all of the datasource delegate methods which would then have your data in it.

Resources