Hello I'm new to developing and I was wondering if any of you pros would know how to fix this issue.
My code is Below: InstaTwitViewController.m:
#import "InstaTwitViewController.h"
#implementation InstaTwitViewController
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
activities = [[NSArray alloc] initWithObjects:#"sleeping",
#"eating", #"working", #"thinking", #"crying", #"begging",
#"leaving", #"shopping", #"hello worlding", nil];
feelings = [[NSArray alloc] initWithObjects: #"awesome",
#"sad", #"happy", #"ambivalent", #"nauseous", #"psyched",
#"confused", #"hopeful", #"anxious", nil];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[activities release];
[feelings release];
[super dealloc];
}
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)
pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)
pickerViewnumberOfRowsInComponent :(NSInteger)component {
if (component == 0) {
return [activities count];
}
else {
return [feelings count];
}
}
#end
Next to [activities count] and [activities release] it states an error "'activities' undeclared"
InstaTwitViewController.h:
//
// InstaTwitViewController.h
// InstaTwit
//
// Created by John Bridge on 5/2/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface InstaTwitViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> {
NSArray* actvities;
NSArray* feelings;
}
#end
EDIT
You have a typo in your property declaration.
actvities should be activities, with an i.
You should be more careful while coding, and reading your own code...
EDIT END
Apparently, you haven't declared the activities variable. That's why XCode says it's undeclared...
I guess it should be an NSArray... You need to declare the variable in your class interface (the header file).
Something like:
#interface InstaTwitViewController: UIViewController
{
NSArray * activities;
}
#end
Then, in your implementation, you need to allocate it, for instance in the init method:
- ( id )initWithNibName: ( NSString * )nibNameOrNil bundle: ( NSBundle * )nibBundleOrNil
{
if( ( self = [ super initWithNibName: nibNameOrNil bundle: nibBundleOrNil ] ) )
{
activities = [ NSArray new ];
}
return self;
}
And don't forget to release it in the dealloc method:
- ( void )dealloc
{
[ activities release ];
[ super dealloc ];
}
Remove the /* and */ code before and after the viewDidLoad method, and declare an NSArray called activities in your .h file.
NSArray *activities;
EDIT---- As MacMade said, just fix the spelling mistake!
Related
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Making the iPhone vibrate
I have two buttons. One button adds up, one button adds down. The question is when I certain number such as lets say 22 is in the text area, the phone vibrates for a certain mount of time. Here is my code for it:
What I am trying to say is IF Label Displays "22" THEN VIBRATE PHONE... The question is how do i go about writing this.. I'm still learning so any help regarding this would be much appreciated! Here is my code so far:
#import "StartCountViewController.h"
#import "AudioToolbox/AudioServices.h"
#implementation StartCountViewController
int Count=0;
-(void)awakeFromNib {
startCount.text = #"0";
}
- (IBAction)addNumber {
if(Count >= 999) return;
NSString *numValue = [[NSString alloc] initWithFormat:#"%d", Count++];
startCount.text = numValue;
[numValue release];
}
- (IBAction)vibrate {
}
- (IBAction)subtractNumber {
if(Count <= -35) return;
NSString *numValue = [[NSString alloc] initWithFormat:#"%d", Count--];
startCount.text = numValue;
[numValue release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
This is basically a duplicate of Programmatically make the iPhone vibrate
Having said that, I think your code is still going to have errors, and the syntax seems deprecated.
Here's an example. I didn't try this on an actual iphone which would be required to test the vibration, but it should work provided you add the AudioToolbox framework to your project, and of course your XIB file has the necessary connections:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (retain, nonatomic) IBOutlet UILabel *numberLabel;
- (IBAction)addNumber:(id)sender;
- (IBAction)subtractNumber:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "AudioToolbox/AudioServices.h"
#interface ViewController ()
{
int count;
}
#end
#implementation ViewController
#synthesize numberLabel;
- (void)viewDidLoad
{
count = 0;
[self updateCount];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setNumberLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc
{
[numberLabel release];
[super dealloc];
}
- (IBAction)addNumber:(id)sender
{
if(count >= 999) {
return [self vibrate];
}; // ignore numbers larger than 999
count++;
[self updateCount];
}
- (IBAction)subtractNumber:(id)sender
{
if(count <= -35) {
return [self vibrate];
}; // ignore numbers less than -35
count--;
[self updateCount];
}
-(void)updateCount
{
NSString *countStr = [[NSString alloc] initWithFormat:#"%d",count];
[self.numberLabel setText:countStr];
[countStr release];
}
-(void)vibrate
{
NSLog(#"I'm vibrating");
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
#end
I try to save numbers from a textfield on one viewController in a NSMutableArray when I press a button on this viewContoller. (this is working now)
Then i want the numbers give out on a textview which is on a secondViewController but this dont work. When i want to give out the array on the first Viewcontroller it work fine.
Also i cant erase the NSMutableArray with a button on the SecondviewController.
The SecondviewController have the same class like the viewController.
Can someone show me how i can give out an array on a seconviewcontroller?
Hallo,
at the moment i have this:
//ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
NSMutableArray *textViewArray;
}
#property (strong, nonatomic) IBOutlet UITextField *textLable2;
#property (strong, nonatomic) IBOutlet UITextField *textLable1;
- (IBAction)setArrayWithCurrentNumber:(id)sender;
- (IBAction)returnToTextfield:(id)sender;
#end
//this the .m file:
#import "ViewController.h"
#implementation ViewController
#synthesize textLable2;
#synthesize textLable1;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
textViewArray = [[NSMutableArray alloc]init];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setTextLable2:nil];
[self setTextLable1:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)setArrayWithCurrentNumber:(id)sender
{
NSString *string1 = self.textLable1.text;
[textViewArray addObject:string1];
NSMutableArray *array = [NSMutableArray arrayWithArray:textViewArray];
NSString *string2 = [array componentsJoinedByString:#" "];
self.textLable2.text = [NSString stringWithString:string2];
NSLog(#"%#",textViewArray);
}
- (IBAction)returnToTextfield:(id)sender
{
[textLable1 resignFirstResponder];
[textLable2 resignFirstResponder];
}
#end
If you're calling the second view controller from the first one you could set a property on the second one to hold the NSMutableArray or just send it on the initializer.
something like:
- (id)initWithArray:(NSMutableArray *)array {
if (self = [super init]) {
myArray = [array copy];
}
return self;
}
assuming your second view controller has declared NSMutableArray * myArray;
Edit: Adding some more code in here...
// I'll assume you use some kind of UINavigationController to show your content
- (void)showSecondViewController {
SecondViewController * vc = [[[SecondViewController alloc] initWithArray:yourMutableArray] autorelease];
[self.navigationController pushViewController:vc];
}
The title of this question should be pretty self explanatory. I am making an app that involves multiple UIImageViews that serve the same purpose. They are merely different sizes. Anyway, I decided that the best solution was to make UIImageView subclasses, link the subcalsses in IB, and work from there. My code should explain this better -
#define kPausedStatePaused 1
#define kPausedStatePlay 2
#import "Game.h"
#import "ScoreSystem.h"
#interface Doctor : UIImageView
{
}
#end
#interface Ground : UIImageView
{
}
#end
#interface Wall : UIImageView
{
}
#end
#interface Electric_Wire : UIImageView
{
}
#end
#implementation Game
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
- (IBAction)pause {
UIAlertView *pause = [[UIAlertView alloc] initWithTitle:#"Pause" message:nil delegate:self cancelButtonTitle:#"Quit" otherButtonTitles:#"Play", nil];
[pause show];
[pause release];
pauseint = kPausedStatePaused;
}
- (void)viewDidAppear {
pauseint = kPausedStatePlay;
}
- (void)loop {
Doctor *doctorview;
Ground *groundview;
if (CGRectIntersectsRect(doctorview, groundview)) {
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView.title isEqual:#"Pause"]) {
if(buttonIndex == 0)
[self dismissModalViewControllerAnimated:YES];
pauseint = kPausedStatePlay;
}
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Unsurprisingly, Xcode gave me an "incompatible type for CGRectIntersectsRect" error.
You must pass view's frame, not the view's themselves to that function:
CGRectIntersectsRect(doctorview.frame, groundview.frame)
My application is crashing, I think, in RootController.m and I don't know why. It occurs when I am in any view controller and I push the back button. It briefly returns to RootController and then it crashes. There is no messages on the console. I don't think it is the ViewController as I have tried more than one.
Here is the code.
#import "confirmViewController.h"
#implementation confirmViewController
#synthesize lblStatus;
#synthesize lblCardType;
#synthesize lblCardNumber;
#synthesize lblExpires;
#synthesize lblAmount;
#synthesize lblApproval;
#synthesize strConfirmation;
#synthesize strCardNumber;
#synthesize strExpires;
#synthesize strAmount;
#synthesize strApproval;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//prepare confirmation, all that is needed is the first string
NSArray *strings = [strConfirmation componentsSeparatedByString: #","];
NSString *strPreParsed = [strings objectAtIndex:0];
//break out yes/no so we can set status
//NSString *strYesNO = [strPreParsed substringToIndex:2];
NSString *strYesOrNo = [strPreParsed substringWithRange: NSMakeRange(1, 1)];
//save approval for later
strApproval = strYesOrNo;
//debug
NSLog(#"strNo= %#",strYesOrNo);
NSLog(#"strPreParsed= %#", strPreParsed);
if([strYesOrNo compare:#"Y"] == NSOrderedSame)
{
lblStatus.text = #"Approved";
lblStatus.textColor = [UIColor greenColor];
}
if([strYesOrNo compare:#"N"] == NSOrderedSame)
{
lblStatus.text = #"Declined";
lblStatus.textColor = [UIColor redColor];
}
if([strYesOrNo compare:#"U"] == NSOrderedSame)
{
lblStatus.text = #"Try Again";
lblStatus.textColor = [UIColor redColor];
}
//set card type
if([lblCardNumber.text compare:#"4"] == NSOrderedSame)
{
lblCardType.text = #"Visa";
}
if([lblCardNumber.text compare:#"5"] == NSOrderedSame)
{
lblCardType.text = #"Master";
}
if([lblCardNumber.text compare:#"6"] == NSOrderedSame)
{
lblCardType.text = #"Discover";
}
//set cardnumber
lblCardNumber.text = strCardNumber;
//set expires
lblExpires.text = strExpires;
//set amount
lblAmount.text = strAmount;
//set approval string
lblApproval.text = strPreParsed;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
//show signature
sigCaptureViewController *yetAnotherViewController = [[sigCaptureViewController alloc] initWithNibName:#"sigCaptureView" bundle:nil];
[self.navigationController pushViewController:yetAnotherViewController animated:YES];
[yetAnotherViewController release];
}
- (void)dealloc {
[super dealloc];
[lblCardType dealloc];
[lblCardNumber dealloc];
[lblExpires dealloc];
[lblAmount dealloc];
[lblApproval dealloc];
[lblStatus dealloc];
[strConfirmation dealloc];
[strCardNumber dealloc];
[strExpires dealloc];
[strAmount dealloc];
[strApproval dealloc];
}
#end
You are most likely releasing something twice in your SalesViewController, it's crashing when that object's dealloc is getting called and doing the 2nd release.
You didn't include that contorller's code, either look for it yourself or paste it here and I'll help you spot it :)
Here's a scenario you should look for:
// somewhere within your sales controller
- (void)someWhere {
NSArray *arr = [NSArray array];
self.myArray = arr;
[arr release]; // notice how arr wasn't initialized with an init
// so no release is required
// but since your myArray is a #property(retain) it won't crash here
// because the property did a retain
}
- (void)dealloc {
[myArray relase]; // this does the 2nd release and BOOM
}
EDIT after your target view controller was pasted:
It might be because you forgot to set one of the properties you're releasing in the dealloc. Try setting a breakpoints in your dealloc and see if one of the properties is actually null before releasing it, if so, that's why it crashes.
EDIT #2, are you sure you want to be calling [lblSometing dealloc] instead of [lblSomething release]?
In my hello world application, i have a button and a text field set up. You enter your name in the text field then press the button, then you're supposed to see "Hello, [name]!". However all i get is "Hello, World!" (the default for when there's no string in the text box), even when i input a name. As requested, here's the file:
//
// MyViewController.m
// HelloWorld
//
// Created by RCIX on 7/10/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "MyViewController.h"
#implementation MyViewController
#synthesize textField;
#synthesize label;
#synthesize string;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[textField release];
[label release];
[string release];
[super dealloc];
}
- (IBAction)changeGreeting:(id)sender {
self.string = textField.text;
NSString *nameString = self.string;
if ([nameString length] == 0) {
nameString = #"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:#"Hello, %#!", nameString];
label.text = greeting;
[greeting release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
#end
Double-check your connections in Interface Builder. If textField isn't connected properly, then your method won't be able to read the text property of the field right, which may lead to its having a length of 0.
There is no need to make nameString = string, just work on [self string] directly!
Here a working example:
string = textField.text;
if ([string length] == 0) {
string = #"World";
}
NSString *greeting = [NSString stringWithFormat:#"Hello, %#!", string];
[label setStringValue:greeting];
You'll see I changed initWithFormat to stringWithFormat. If you use stringWithFormat, the NSString object is autoreleased, so we don't have to worry about releasing it.
One last thing... I don't really see the need for having "string" be an instance variable, as it is just used temporarily... it might be better to just go
NSString *string = textField.text;
and not have it declared in the #interface... just a thought.
Is the text field hooked up properly in Interface Builder?