Hello everybody, I have a UITableView in a UIViewController. When a row in the table is tapped I am collecting the cell's text value and putting it in a string called localstringGValue.
I want to pass this string and display it in another, viewController, but without using -pushViewController: I want this value to be stored somewhere like NSUserDefaults or NSDictonary and then, on viewWillapper of the other view controller I want this stored value to be displayed in a label.
in my .h:
NSString *localStringGValue;
in my .m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
localStringGValue = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
}
in my other view controller:
-(void)viewWillAppear
{
label.text=localStringGValue;
}
Thanks in advance.
save to nsuserdefaults:
[[NSUserDefaults standardUserDefaults] setObject:localstringGValue forKey:#"localstringGValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
retrieve from nsuserdefaults:
NString *localstringGValue = [[NSUserDefaults standardUserDefaults] objectForKey:#"localstringGValue"];
Just use delegate. Before you push the 'UploadViewController' instance, you need set it's delegate as self(in GoogleDocMainPageController.m). Everytime, the tabel cell is selected, it'll set value for self.delegate(Here is GoogleDocMainPageController instance) by dispatching self.delegate's method, which is implemented by GoogleDocMainPageController:
[self.delegate setDataAfterSelectedTabelCell:[NSString stringWithFormat:#"TalbeCell %d selected", [indexPath row]]];
The main code is shown below:
UploadViewController.h:
#import <UIKit/UIKit.h>
#class UploadViewController;
#protocol UploadViewControllerDelegate <NSObject>
- (void)setDataAfterSelectedTabelCell:(NSString *)stringValueInCell;
#end
#interface UploadViewController : UITableViewController
#property (nonatomic, retain) id <UploadViewControllerDelegate> delegate;
#end
UploadViewController.m:
//...
#synthesize delegate = _delegate;
//...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.delegate setDataAfterSelectedTabelCell:[NSString stringWithFormat:#"TalbeCell %d selected", [indexPath row]]];
}
GoogleDocMainPageController.h:
#import <UIKit/UIKit.h>
#import "UploadViewController.h"
#class UploadViewController;
#interface GoogleDocMainPageController : UIViewController <UploadViewControllerDelegate>
- (void)loadUploadViewController;
#property (nonatomic, retain) UILabel * glLabel;
#property (nonatomic, retain) UploadViewController * uploadViewController;
#end
GoogleDocMainPageController.m:
//...
#synthesize glLabel = _glLabel;
#synthesize uploadViewController = _uploadViewController;
//...
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton * uploadButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 160.0f, 300.0f, 35.0f)];
[uploadButton setBackgroundColor:[UIColor blackColor]];
[uploadButton setTitle:#"Upload Button" forState:UIControlStateNormal];
[uploadButton addTarget:self action:#selector(loadUploadViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:uploadButton];
[uploadButton release];
self.glLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 200.0f, 300.0f, 35.0f)];
[self.glLabel setBackgroundColor:[UIColor blackColor]];
[self.glLabel setTextColor:[UIColor whiteColor]];
[self.glLabel setTextAlignment:UITextAlignmentCenter];
[self.glLabel setText:#"Default"];
[self.view addSubview:self.glLabel];
self.uploadViewController = [[UploadViewController alloc] initWithStyle:UITableViewStylePlain];
}
//...
#pragma mark -
- (void)loadUploadViewController
{
[self.uploadViewController setDelegate:self];
[self.navigationController pushViewController:self.uploadViewController animated:YES];
}
#pragma mark - UploadViewControllerDelegate
- (void)setDataAfterSelectedTabelCell:(NSString *)stringValueInCell
{
[self.glLabel setText:stringValueInCell];
}
Why don't you declare some #property(nonatomic,retain) and #synthesize it ? Then the getters/setters will be automatically created. Otherwise, define some getter/setters yourself.
Go through Following Steps-
In First ViewController -
Create Object of second ViewController
like SecondViewController *sec= [[SecondViewController alloc]init];
sec.myLable=#"My Lable String";
In Second ViewController -
In .h file
UILable *myLable;
#property(nonautomic,retain)IBOutlet UILable *myLable;
2.In .m file
#synthesize myLable;
Related
I have inherited some old iOS code and have attempted to integrate it into a new iOS 6 application. I have implemented most of the code and so far everything has worked. I'm now working on the last bit of that old code. I'm implementing a set of views to show a rss for a news section of my app. I've implemented the categories view, which upon selecting an item would display the individual items within that category. However nothing gets displayed. I've made all the modifications that I'm aware of that I needed to do, however I'm no expert at iOS development and am in need of some guidance. Below is a snapshot of the simulator as it's attempting to display the view, and below that is a copy of my .h and .m files. I don't know what is preventing anything in the table from showing up. And preemptive thanks to any help!
here's the snapshot of the simulator
Here is a snapshot of the storyboard showing the linking to the Table View
Here's the .h file
#import <UIKit/UIKit.h>
#import "BlogRssParser.h"
#class BlogRssParser;
#class BlogRssParserDelegate;
#class BlogRss;
#class XMLCategory;
#interface NewsViewController : UIViewController <UITableViewDataSource,UITableViewDelegate, BlogRssParserDelegate> {
BlogRssParser * _rssParser;
XMLCategory * _currItem;
}
#property (nonatomic, retain) BlogRssParser * rssParser;
#property (readwrite, retain) XMLCategory * currItem;
#property (nonatomic, retain) IBOutlet UITableView *itemTableView;
#end
Here is my .m file
#import "NewsViewController.h"
#import "NewsDetailsViewController.h"
#import "BlogRssParser.h"
#import "BlogRss.h"
#import "XMLCategory.h"
#define kLabelTag 1;
#interface NewsViewController ()
#end
#implementation NewsViewController
#synthesize rssParser = _rssParser;
#synthesize currItem = _currItem;
- (void)navBarInit {
UIBarButtonItem *refreshBarButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:#selector(reloadRss)];
[self.navigationItem setRightBarButtonItem:refreshBarButton animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.itemTableView.delegate = self;
self.itemTableView.dataSource = self;
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self navBarInit];
[self.itemTableView reloadData];
self.itemTableView.userInteractionEnabled = NO;
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
_rssParser = [[BlogRssParser alloc]init];
_rssParser.delegate = self;
[[self rssParser]startProcess:[_currItem categoryId]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)reloadRss{
[[self rssParser]startProcess:[_currItem categoryId]];
[[self itemTableView]reloadData];
}
- (void)processCompleted{
[[self itemTableView]reloadData];
// _tableView.userInteractionEnabled = YES;
[[self itemTableView]setUserInteractionEnabled:YES];
}
-(void)processHasErrors{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"My Title" message:#"Unable to retrieve the news. Please check if you are connected to the internet."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[[self rssParser]rssItems]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
const CGFloat LABEL_TITLE_HEIGHT = 70.0;
const CGFloat LABEL_WIDTH = 210.0;
NSString * mediaUrl = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]mediaUrl];
NSData * imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mediaUrl]];
UIImage * imageFromImageData;
if (imageData == nil) {
imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:#"http://www.urlForImage.image.png"]];
}
imageFromImageData = [[UIImage alloc] initWithData:imageData];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"rssItemCell"];
if(nil == cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"rssItemCell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel * _topLabel =
[[UILabel alloc]
initWithFrame:
CGRectMake(
imageFromImageData.size.width + 10.0,
0.0,
LABEL_WIDTH,
LABEL_TITLE_HEIGHT)];
_topLabel.tag = kLabelTag;
_topLabel.opaque = NO;
_topLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
_topLabel.backgroundColor = [UIColor clearColor];
_topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
_topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
_topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
_topLabel.numberOfLines = 0;
[cell.contentView addSubview:_topLabel];
}
cell.imageView.image = imageFromImageData;
UILabel * topLabel = (UILabel *)[cell.contentView viewWithTag:1];
topLabel.text = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]title];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NewsDetailsViewController *tlc = [[DetailsViewController alloc]init];
tlc.currentItem = [[[self rssParser]rssItems]objectAtIndex:indexPath.row];
tlc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:tlc animated:YES completion:nil];
}
#end
I could not get a conclusion about the problem you are facing.
But here are few things you should check.
Because i cannot see even an empty table view in your screenshot
Do you have the TableView on the Nib file ?
It is mapped from The Nib file to the IBOutlet itemTableView ?
Add at least one table view cell (Drag and drop one prototype cell)..
like this
Then select that cell and give some name in "Reuse Identifier" with this identifier allow datasource..
First thing I would do is make sure that [[[self rssParser] rssItems] count] is actually returning > 0. Also, is this a copy&paste of your .m file? viewDidLoad is missing the closing brace, but Xcode would catch that.
I have implemented UIPopoverController with storyboard but i am not able to make it dismiss when I select particular row in UITableView.
When select particular row so that time I want to dismiss the popover but I am not able dismiss it.
I write below code for this:
//Show the popover in Main UIViewController
-(IBAction)clickNotes:(id)sender {
NSLog(#"notes:");
NoteList *objNoteList = [[NoteList alloc] initWithNibName:#"NoteList" bundle:nil];
popover.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.popoverContentSize = CGSizeMake(250, 450);
[popover presentPopoverFromRect:CGRectMake(730, 0, 1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
//Hide the popover in another UIViewController on didSelecteRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Notepad_ipad *objNote = [[Notepad_ipad alloc] init];
NSString *mSelectedNoteText = #"Selected text";
[objNote SelectedNote:mSelectedNoteText];
[objNote.popover dismissPopoverAnimated:YES];
}
use
[popover dismissPopoverAnimated:YES];
The following code instantiates a NEW Instance. So it has nothing to do with the already existing popover :Notepad_ipad *objNote = [[Notepad_ipad alloc] init];
Also Instead Of :
popover.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
USE:
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.delegate = self;
I.e.: Allocate the instance first and then set its delegate.
Finally replace your method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
with this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {
[popover dismissPopoverAnimated:YES];
}
// Create protocol in .h file of controller which contains didSelectRowAtIndexPath method as follows:
#protocol Popoverdelegate <NSObject>
{
-(void)didRowAtIndexPathIsSelected;
}
// Add this property in .h file of the same controller
#property (strong, nonatomic) id<Popoverdelegate> delegate;
// Now implement this protocol in interface which calls popovercontroller
// for ex: #interface ViewController <Popovercontroller>
// then add following properties to viewController .h file
#protocol (strong, nonatomic) UIPopoverController *popoverController;
// Implement popoverdelegate protocol in .m file as
- (void) didRowAtIndexPathIsSelected
{
[self.popoverController dismissPopoverAnimated:YES];
}
// Replace your code as follows
-(IBAction)clickNotes:(id)sender
{
NoteList *objNoteList = [[NoteList alloc] initWithNibName:#"NoteList" bundle:nil];
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.delegate = self;
self.popoverController = popover;
self.popoverController.popoverContentSize = CGSizeMake(250, 450);
[self.popoverController presentPopoverFromRect:CGRectMake(730, 0, 1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Notepad_ipad *objNote = [[Notepad_ipad alloc] init];
NSString *mSelectedNoteText = #"Selected text";
[objNote SelectedNote:mSelectedNoteText];
[self.delegate dismissPopoverAnimated:YES];
}
The smartest thing to do here (imho) is to follow this example code, I do it every time:
// firstViewController.h
#interface firstViewController : UIViewController <SecondDelegate>
{
SecondViewController *choice;
}
// firstViewController.m
- (void)choicePicked(NSString *)choice
{
NSLog(choice);
[_popover dismissPopoverAnimated:YES]; //(put it here)
_popover = nil; (deallocate the popover)
_choicePicker = nil; (deallocate the SecondViewController instance)
}
// secondViewController.h (the one that will give back the data)
#protocol SecondDelegate <NSObject>
- (void)choicePicked:(NSString *)choice;
#end
#interface secondViewController : UITableViewController
#property (nonatomic, assign) id <SecondDelegate> delegate;
#end
// secondViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selection = [_yourArray objectAtIndex:indexPath.row];
[[self delegate] choicePicked:selection];
}
I hope you can help me to set the background color of my navigation bar. As you can see on the first picture below, the bar is light blue/gray, but i want it to be like the second picture "Favoritter".
I have another viewController in my project (see the second picture "Favoritter") where it works. But I can not see what I'm doing wrong here and the navigationBar will only take this light blue/gray color and not yellow.
My code is like this:
------------------InstantCabAppDelegate.m file-----------START-------
//InstantCabAppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Create tabBarController instance
tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
// Create instances of navigation controller and view controllers
orderController = [[OrderController2 alloc] initWithNibName:#"Order2" bundle:nil];
UINavigationController* firstnavigationController = [[UINavigationController alloc] initWithRootViewController:orderController];
ICStatusViewController *statusController = [[ICStatusViewController alloc] initWithStatus];
UINavigationController *statusNavController = [[UINavigationController alloc] initWithRootViewController:statusController];
ICSettingsViewController *settingsController = [[ICSettingsViewController alloc] init];
UINavigationController *settingsNavController = [[UINavigationController alloc] initWithRootViewController:settingsController];
UIViewController *company_controller = nil;
company_controller = [[ICCompaniesGridController alloc] initWithCompanies:[[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Companies" ofType:#"plist"]]];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstnavigationController, settingsNavController, statusNavController, company_controller, nil];
[window setRootViewController:tabBarController];
[window makeKeyAndVisible];
return YES;
}
------------------ICStatusViewController.m file-----------START-------
// ICStatusViewController.m file
#import "ICStatusViewController.h"
#import "LocalizationSystem.h"
#import "ICTemporaryStore.h"
#import "Debug.h"
#implementation ICStatusViewController
#synthesize doneCallbackBlock=_doneCallbackBlock, cancelCallbackBlock=_cancelCallbackBlock;
#synthesize myTableView, no_statuses, no_statuses_label, delegate;
- (id)initWithStatus
{
if ((self = [super initWithNibName:#"StatusList" bundle:nil]))
{
self.title = AMLocalizedString(#"kStatusTitle", #"");
self.tabBarItem.image = [UIImage imageNamed:#"status_icon"];
self.no_statuses = AMLocalizedString(#"kStatusListNoStatusList", #"");
first_color = TABLE_VIEW_BACKGROUND_COLOR;
second_color = first_color;
self.doneCallbackBlock = nil;
self.cancelCallbackBlock = nil;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background-nologo"]];
self.view.backgroundColor = [UIColor clearColor];
self.myTableView.backgroundColor = TABLE_VIEW_BACKGROUND_COLOR;
if ([statusObjects count] == 0)
{
self.myTableView.hidden = YES;
self.no_statuses_label.text = self.no_statuses;
self.no_statuses_label.hidden = NO;
no_status_background.hidden = NO;
}
[self.no_statuses_label setTextColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([statusObjects count] == 0)
{
self.myTableView.hidden = YES;
self.no_statuses_label.text = self.no_statuses;
self.no_statuses_label.hidden = NO;
no_status_background.hidden = NO;
self.navigationItem.rightBarButtonItem = nil;
}
else
{
self.myTableView.hidden = NO;
self.no_statuses_label.hidden = YES;
no_status_background.hidden = YES;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger retval = 0;
for (int i=0; i<[statusObjects count]; i++)
{
StatusObj *add = [statusObjects getStatusAtIndex:i];
if (add.status_id && [add.status_id length] > 0 && [add.status_type isEqualToString:#"STATUS_ORDER"])
{
retval++;
}
}
return retval;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierStatus = #"StatusCell";
__unsafe_unretained NSString *CellIdentifier = CellIdentifierStatus;
StatusCell* cell = (StatusCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[StatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[[cell textLabel] setTextColor:TABLE_VIEW_TEXT_LABEL_COLOR];
[[cell detailTextLabel] setTextColor:TABLE_VIEW_DETAIL_TEXT_LABEL_COLOR];
[[cell detailTextLabel] setNumberOfLines:3];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
dateString = [dateFormat stringFromDate:current_order.order.time];
NSString *str = AMLocalizedString(#"kStatusListDetailsToDay", #"");
textLabelString = [NSString stringWithFormat:#"%# - %#", str, dateString];
[cell.textLabel setText:textLabelString];
[cell.detailTextLabel setText:detailTextString];
[cell setBackgroundColor:[UIColor redColor]];
[cell.dateLabel setBackgroundColor:[UIColor clearColor]];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
------------------ICStatusViewController.h file-----------START-------
// ICStatusViewController.h
#import <UIKit/UIKit.h>
#import "Status.h"
#import "StatusCell.h"
#import "StatusControllerDelegate.h"
#import "ICStatusDetailsController.h"
typedef void (^ICStatusViewControllerDoneCallbackBlock)(id userInfo);
typedef void (^ICStatusViewControllerCancelCallbackBlock)(void);
#interface ICStatusViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate, ICStatusDetailsControllerDelegate> {
id<Statuses> statusObjects;
NSString *no_statuses;
NSOperationQueue *queue;
NSTimer *statusTimer;
__unsafe_unretained id<StatusControllerDelegate> delegate;
UIColor *first_color;
UIColor *second_color;
IBOutlet __unsafe_unretained UITableView *myTableView;
IBOutlet __unsafe_unretained UILabel *no_statuses_label;
IBOutlet __unsafe_unretained UIImageView *no_status_background;
#private
ICStatusViewControllerDoneCallbackBlock _doneCallbackBlock;
ICStatusViewControllerCancelCallbackBlock _cancelCallbackBlock;
}
#property (nonatomic, unsafe_unretained) UITableView* myTableView;
#property (nonatomic, copy) NSString* no_statuses;
#property (nonatomic, unsafe_unretained) UILabel* no_statuses_label;
#property (nonatomic, unsafe_unretained) id <StatusControllerDelegate> delegate;
#property (nonatomic, copy) ICStatusViewControllerDoneCallbackBlock doneCallbackBlock;
#property (nonatomic, copy) ICStatusViewControllerCancelCallbackBlock cancelCallbackBlock;
- (id)initWithStatus;
#end
You can either set the tint color of the navigation bar OR use a IMAGE instead of navigation bar and hide the navigation bar, using:
navigationController.navigationBarHidden = YES;
You are not talking about a UITableView, but about the UINavigationBar on the top. I fixed your question to reflect that. Basically there are two ways to set its tintColor:
The easier option would be to use the appearance protocol (iOS 5 and later), as this will change every NavigationBar in the whole app:
[[UINavigationBar appearance] setTintColor:[UIColor yellowColor]];
Or you do it on every single UINavigationBar of every UINavigationController.
[navigationController.navigationBar setTintColor:[UIColor yellowColor]];
just add this line for set yellow color to navigationcontroller in viewDidLoad: method of ICStatusViewController class..
[self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];
and also with image like bellow..
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"yourImageName"] forBarMetrics:UIBarMetricsDefault];// write your image name like yellow image
i hope this help you...
What's your tableView's style? If tableView's style is UITableViewStyleGrouped, you can't change backgroundColor on iOS 6. Try to change tableView's backgroundView.
self.settingsTableView = [[UITableView alloc] initWithFrame:rect
style:UITableViewStyleGrouped];
UIView *view = [[UIView alloc] initWithFrame:settingsTableView.frame];
view.backgroundColor = [UIColor redColor];
settingsTableView.backgroundView = view;
How would this work in this case? I created a NSMutabeArray *dataSource; in my .h file, but getting a bunch of errors:
"RootViewController.m: error: Semantic Issue: Property 'dataSource' not found on object of type 'RootViewController *'"
RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController {
NSMutableArray *dataSource;
}
#property (nonatomic,retain) NSMutableArray *dataSource;
- (IBAction)addButton:(id)sender;
#end
RootViewController.m
#import "RootViewController.h"
#implementation RootViewController
#synthesize dataSource;
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataSource = [NSMutableArray arrayWithCapacity:1];
//adds right bar button.
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem=addButton;
[addButton release];
}
-(void)addButton:(id)sender{
[self.dataSource addObject:#"New Item"];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[self.dataSource count] inSection:0];
[self.dataSource insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Some memory-guru will no doubt be able to tell you why #synthesize and mutable arrays and dictionaries (and sets, presumably) do not play well together. All I know is, initialize your mutable array explicitly and all will be well:
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataSource = [NSMutableArray arrayWithCapacity:1];
//adds right bar button.
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem=addButton;
[addButton release];
}
And, of course, release it in the dealloc.
You have not created property in .h file and you have used datasource variable with self.
please replace self.dataSource with dataSource or create property for it.
#property (nonatomic,retain) NSMutableArray *dataSource;
and synthesize in .m file
#synthesize dataSource;
I am using code from Ed Marty's answer for the question here but am having real trouble with a few bits.
On the click of a button I have got the datepicker appearing, but the 'done' button however isn't. I am also getting an error from the line:
[delegate datePickerController:controller didPickDate:datePicker.date];
Error message:
'controller' undeclared (first use in this function)
All in all I have 6 files:
ModalDatePickerViewController.m
ModalDatePickerViewController.h
ModalDatePickerAppDelegate.m
ModalDatePickerAppDelegate.h
DatePickerController.m
DatePickerController.h
My DatePickerController.h is looking like:
#import <UIKit/UIKit.h>
#class DatePickerController;
#protocol DatePickerControllerDelegate
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date;
#end
#interface DatePickerController : UIViewController {
UIDatePicker *datePicker;
NSObject <DatePickerControllerDelegate> *delegate;
}
#property (nonatomic, retain) UIDatePicker *datePicker;
#property (nonatomic, assign) NSObject <DatePickerControllerDelegate> *delegate;
#end
and the DatePickerController.m:
#import "DatePickerController.h"
#implementation DatePickerController
#synthesize datePicker;
#synthesize delegate;
- (void) loadView {
self.view = [[[UIView alloc] init] autorelease];
self.datePicker = [[[UIDatePicker alloc] init] autorelease];
[self.view addSubview:self.datePicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.center = CGPointMake(160,230);
[button addTarget:self action:#selector(done) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
- (void) done {
[delegate datePickerController:controller didPickDate:datePicker.date];
}
- (void) dealloc {
[datePicker release];
[super dealloc];
}
#end
On the main view I have a button that call this class as below:
#import "ModalDatePickerViewController.h"
#implementation ModalDatePickerViewController
- (void) pickDate {
DatePickerController *screen = [[[DatePickerController alloc] init] autorelease];
screen.delegate = self;
[self presentModalViewController:screen animated:YES];
}
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date {
//[self doSomethingWithDate:date];
[controller dismissModalViewControllerAnimated:YES];
}
- (IBAction)HitMe:(id)sender {
[self pickDate];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
and:
#import <UIKit/UIKit.h>
#import "DatePickerController.h"
#interface ModalDatePickerViewController : UIViewController <DatePickerControllerDelegate> {
}
- (IBAction)HitMe:(id)sender;
#end
On this line:
[delegate datePickerController:controller didPickDate:datePicker.date];
try replacing controller with self.