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;
Related
I made a application with the templet of a Single View Application. Then I added a label and connected it to the .h file of my ViewController. Then I made a picker, filled it, then set it (and a toolBar I made) to the textfield. But when I tap the textfield, the picker is just all black. If this made no sense, the code will explain it all.
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
}
#property (weak, nonatomic) IBOutlet UITextField *habitField;
#property (weak, nonatomic) NSArray *PickerData;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:#"1",#"2",#"3", nil];
self.PickerData = array;
UIToolbar *toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlackOpaque;
[toolBar sizeToFit];
[toolBar setBackgroundImage:[UIImage imageNamed:#"red_navigation_bar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(releasePicker)];
UIPickerView *Picker = [[UIPickerView alloc] init];
doneButton.image = [UIImage imageNamed:#"button.png"];
[toolBar setItems:#[flexSpace, doneButton] animated:YES];
self.habitField.inputAccessoryView = toolBar;
[self.habitField setInputView:Picker];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.PickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.PickerData objectAtIndex:row];
}
#end
The simulator looks like this
As i see you forgot to set delegate for picker ( UIPickerViewDelegate )
Picker.delegate = self;
Remember to add :)
#interface ViewController : UIViewController<UIPickerViewDelegate> {
}
Cheers
I have a subclass of a UITableViewCell which is crashing when it hits the [super dealloc] line. I have several textFields in my cell and the error message is saying *** -[UITextField release]: message sent to deallocated instance 0x739dfd0
The relevant code snippets are below (I do have other textFields but they are all treated the same way. My suspicion is it is to do with adding it to the contentView of the cell. But I do not know how to correct it.
.h file of Custom UITableViewCell:
#interface ExerciseTableViewCell : UITableViewCell {
UITextField *textField1;
}
#property (nonatomic, retain) UITextField *textField1;
#end
.m file:
#implementation ExerciseTableViewCell
#synthesize textField1;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UIView *myContentView = self.contentView;
UITextField *newTextField1 = [[UITextField alloc] init];
self.textField1 = newTextField1;
[newTextField1 release];
[myContentView addSubview:textField1];
}
return self;
}
}
- (void)dealloc {
[textField1 release];
[super dealloc];
}
I cannot see why I would be releasing the textField one too many times?
Change:
UITextField *newTextField1 = [[UITextField alloc] init];
self.textField1 = newTextField1;
[newTextField1 release];
[myContentView addSubview:textField1];
to:
self.textField1 = [[[UITextField alloc] init] autorelease];
[myContextView addSubview:self.textField1];
what is the need of declaring textfield locally and assigning it to the globally declared text field, just use
textField1 = [[UITextField alloc] init];
[myContentView addSubview:textField1];
Set textfield object to nil instead of releasing it. Second, please use a proper naming convention while coding.
I want is on main window to present the created modalViewController view when the infobutton is pressed. But when I press Info button on the main window nothing happens.
In the mainviewcontroller.h file I have following code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface imageviewViewController : UIViewController{
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) UIToolbar *toolbar;
#property (nonatomic, assign) UIBarButtonSystemItem currentSystemItem;
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
#end
In the mainviewcontroller.m file have following code:
#import "imageviewViewController.h"
#import "Infoviewcontroller.h"
#implementation imageviewViewController
#synthesize toolbar;
#synthesize currentSystemItem;
#synthesize audioPlayer;
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
}
- (void) Infobuttonpressed: (id) sender
{
Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
[self presentModalViewController:myView animated:YES]; // present view modally
[self.navigationController pushViewController:myView animated:YES]; // add to navigation stack
[myView release];
}
In the Infoviewcontroller.h file have following code:
#import <UIKit/UIKit.h>
#interface Infoviewcontroller : UIViewController <UITextViewDelegate>
{
UITextView *textView;
}
#property (nonatomic, retain) UITextView *textView;
#property (nonatomic, assign) UINavigationBar *navBar;
#end
Then in the infoviewcontroller.m file have the following code:
#import "Infoviewcontroller.h"
#implementation Infoviewcontroller
#synthesize textView;
#synthesize navBar;
-(void)dealloc
{
[textView release];
[navBar release];
[super dealloc];
}
-(void)setupTextView
{
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor redColor];
self.textView.font = [UIFont fontWithName:#"System Bold" size:13];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView\nThis is UITextView\nThis is UITextView\nThis is UITextView";
[self.view addSubview: self.textView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
navBar = [[UINavigationBar alloc] init];
UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:#"ModalViewControllerTest"] autorelease];
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
navItem.rightBarButtonItem = done;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
Should be
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(Infobuttonpressed:)];
Spot the difference? First, you're missing a target (should be self, not nil). Second, the colon at the end of the selector is part of the signature, you were missing it so it was not calling your method (which is InfoButtonPressed:(id)sender).
As an aside, methods should start with a lower case name.
you dont need both pushModalViewController and pushViewController
Assuming your views are already controlled within an instance of a UINavigationController pushModalViewController on its own will work.
edit - remove the reference to pushViewController. Does it now work?
-(void) Infobuttonpressed: (id) sender;
{ Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myView];
UIViewController *pushController = [[UIViewController alloc] init];
[self addSubview:pushController.view];
[pushController presentModalViewController:navController animated:YES];
[myView release];
}
The target of your infoItem is set to nil. You should set this to an instance of the class where the Infobuttonpressed is defined. I assume that is the mainviewController?
Also, the selector to call is specified as: #selector(Infobuttonpressed), while the method is called "Infobuttonpressed:", that is, with a parameter. That will likely result in a runtime error once you fix the target.
Then, there are some peculiarities in the way you handle things. As Nik already answered, you need to choose whether you want to present the view modally (with presentModalViewController), or present it as part of the navigation stack (with pushViewController), you cannot do both.
If you present it as part of the navitation stack, then it already has a navigation bar, and so you should not add one yourself. Just set the properties of the self.navigationItem.
Have a look at the View Controller Programming Guide for more info.
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;
I having a problem with a Bad Exception that I could not locate at first, but now have it pinned down on a [super dealloc];, but I have no idea why this happens.
Here is my code :
EditingViewController.h
#interface EditingViewController : UIViewController
{
NSManagedObject *editedObject;
NSString *editedFieldKey;
NSString *editedFieldName;
}
#property (nonatomic, retain) NSManagedObject *editedObject;
#property (nonatomic, retain) NSString *editedFieldKey;
#property (nonatomic, retain) NSString *editedFieldName;
- (IBAction)cancel;
- (IBAction)save;
#end
EditingViewController.m
#import "EditingViewController.h"
#implementation EditingViewController
#synthesize editedObject, editedFieldKey, editedFieldName;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
self.title = NSLocalizedString(editedFieldName, nil);
// Configure the save and cancel buttons.
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(save)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (IBAction)save
{
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)cancel
{
// Don't pass current value to the edited object, just pop.
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc
{
[editedObject release];
[editedFieldKey release];
[editedFieldName release];
//[super dealloc];
}
#end
As you can see, I commented the [super dealloc];, which is causing the Bad Exception, but this is obviously not a good solution.
Any idea what I am doing wrong ?
Thanks
The properties might have never been used, so they haven't been initialized in any way
- (void)dealloc
{
self.editedObject = nil;
self.editedFieldKey = nil;
self.editedFieldName = nil;
[super dealloc];
}