Missing context for method declaration? - objective-c

I am trying to create an app and when I added this IBAction, it gave me the error Missing context for method declaration. I have looked up how to fix this but I am a bit confused since I am a beginner. The errors appear in my .m file:
#import "QRscannerSecondViewController.h"
#end
#interface QRscannerSecondViewController ()
#end
#implementation QRscannerSecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Info", #"Info");
self.tabBarItem.image = [UIImage imageNamed:#"second"];
}
return self;
}
- (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.
}
#end
-(IBAction)button1
{
[input resignFirstResponder];
l11 = input.text;
line11.text = [[NSString alloc] initWithFormat:#"%2.f", l11];
}
but I though that it would help if I included my .h file, also.
#import <UIKit/UIKit.h>
#interface QRscannerSecondViewController : UIViewController
#end
#interface TextGameViewController : UIViewController
{
IBOutlet UITextField *input;
IBOutlet UILabel *line11;
NSString *l11;
}
-(IBAction)button1;

You need to put the code:
-(IBAction)button1
{
[input resignFirstResponder];
l11 = input.text;
line11.text = [[NSString alloc] initWithFormat:#"%2.f", l11];
}
before the #end.

Related

How do I stop the background music in another view in Xcode?

So I have a background music playing in one view, then I press the button and go to another view, the background music in another view run as well.
Then I have two background music playing. I want to stop music from the previous view.
So here is the .h code for the first view:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController : UIViewController<AVAudioPlayerDelegate>{
AVAudioPlayer *startingMusic;
}
-(IBAction)StartGame:(id)sender;
So here is the .m code for the first view:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)StartGame:(id)sender{
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *music = [[NSBundle mainBundle] pathForResource:#"Morning_Walk" ofType:#"mp3"];
startingMusic=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
startingMusic.delegate=self;
startingMusic.numberOfLoops=-1;
[startingMusic play];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
here is the .h code for the second view:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface StageOne : UIViewController<AVAudioPlayerDelegate>
AVAudioPlayer *gameMusic;
}
here is the .m code for the second view:
#import "StageOne.h"
#interface StageOne ()
#end
#implementation StageOne
- (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.
NSString *music = [[NSBundle mainBundle] pathForResource:#"Green_Hills" ofType:#"mp3"];
gameMusic=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
gameMusic.delegate=self;
gameMusic.numberOfLoops=-1;
[gameMusic play];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Thanks You so much!
This is normal behavior, as your first controller are still loaded
Try this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear: animated];
NSString *music = [[NSBundle mainBundle] pathForResource:#"Green_Hills" ofType:#"mp3"];
gameMusic=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
gameMusic.delegate=self;
gameMusic.numberOfLoops=-1;
[gameMusic play];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear: animated];
gameMusic.delegate = nil;
[gameMusic stop];
gameMusic = nil;
}

Passing data between two viewcontrollers in xcode using storyboard

I am super new to programming in general. I am trying to make a very simple app.
I am trying to pass data from one viewcontroller to another using a storyboard. On my first screen I have a text field and a button then after you press the button i want the second screen's label to be updated with whatever text you put in the text field.
Here is some of my code:
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize secondviewData;
- (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 from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)passData:(UIButton *)sender {
// SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
self.secondviewData = secondviewData;
secondviewData.passedValue = input.text;
// [self presentViewController:second animated:YES completion:nil];
homeLabel.text = input.text;
}
#end
#import "SecondViewController.h"
#import "FirstViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize passedValue;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
label.text = passedValue;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here's a link to my code: http://oberober.com/passingData/
So far the label on the second screen keeps going blank no matter what I enter on the first screen's text field.
I feel like I am missing an important concept? Any tips or tutorials I can look at?
Thanks ahead of time,
Casper
You pass the data by using the prepareForSegue method of the first controller. Here's an example from one of my apps that shows a vehicle object being passed to the second controller.
CHRBodyViewController is the class of the second controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
CHRBodyViewController *body = [segue destinationViewController];
body.vehicle = _vehicle;
body.updated = YES;
}

property implementation must have its declaration in interface error

"property implementation must have its declaration in interface" error
I have this error coming up and I am new to xcode so I am unsure what to do.
I had lots of code in a class, then i moved it to a different class and now I have numerous errors. Which dont make sense, as everything looks as it should.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize MenuScreenButton;
#synthesize HowToPlayButton;
- (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, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)HowToPlayButtonPressed:(id)sender
{
HowToPlayViewController *HowToPlayView=[[HowToPlayViewController alloc]initWithNibName:nil bundle:nil];
HowToPlayView.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:HowToPlayView animated:YES completion:NULL];
}
-(IBAction)MenuScreenButtonPressed:(id)sender
{
MenuScreen *MenuScreenView=[[MenuScreen alloc]initWithNibName:nil bundle:nil];
MenuScreenView.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:MenuScreenView animated:YES completion:NULL];
}
#end
Below is the .h file:
#import <UIKit/UIKit.h>
#import "HowToPlayViewController.h"
#import "MenuScreen.h"
#interface ViewController : UIViewController
{
IBOutlet UIButton *HowToPlayButton;
IBOutlet UIButton *MenuScreenButton;
}
#property(retain,nonatomic) IBOutlet UIButton *MenuScreenButton;
#property(retain,nonatomic) IBOutlet UIButton *HowToPlayButton;
-(IBAction)HowToPlayButtonPressed:(id)sender;
-(IBAction)MenuScreenButtonPressed:(id)sender;
#end

TabBarController didSelectViewController not working

I know this is a very repeat topic, but I can't get it works.
MainTab.h:
#import <UIKit/UIKit.h>
#interface MainTab : UITabBarController<UITabBarControllerDelegate, UITabBarDelegate> {
IBOutlet UITabBarController *tabController;
}
#property (nonatomic,retain) IBOutlet UITabBarController *tabController;
#end
MainTab.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSLog(#"main tab");
[super viewDidLoad];
self.tabBarController.delegate = (id)self;
[self setDelegate:self];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"selected %d",tabBarController.selectedIndex);
}
I can't find what I'm missing, any help will be appreciated.
Now I try to link it into MainStoryBoard:
But it doesnt work, what are the connection?
On the basis of your #interface (and your subsequent screen snapshot), MainTab is the UITabBarController, so the following line:
self.tabBarController.delegate = (id)self;
Should just be:
self.delegate = self;
You don't want a tabBarController property in the UITabBarController, itself, nor do you want to use the self.tabBarController syntax. You only use that syntax if you're trying to reference the tab bar controller from one of its children controllers. When in the tab bar controller itself, just refer to self.
Thus, it work if MainBar is defined as:
// MainBar.h
#import <UIKit/UIKit.h>
#interface MainBar : UITabBarController
#end
And
// MainBar.m
#import "MainBar.h"
#interface MainBar () <UITabBarControllerDelegate>
#end
#implementation MainBar
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"selected %d",tabBarController.selectedIndex);
}
#end
And don't forget to set the class of the tab bar controller:
The connections inspector (where I didn't touch a thing) looks like:

Passing data from controllerA to controllerB using delegate

I am trying to send simple data from viewControllerA to viewControllerB using delegate but can not find The problem in my code.here is the code
in ViewControllerA.h
#import <UIKit/UIKit.h>
#class ViewControllerA;
#protocol viewControllerADelegate <NSObject>
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item;
#end
#interface ViewControllerA : UIViewController
- (IBAction)buttonPressed:(id)sender;
#property (assign) id<viewControllerADelegate> delegate;
#end
inViewControllerA.m
#import "ViewControllerA.h"
#interface ViewControllerA ()
#end
#implementation ViewControllerA
#synthesize delegate;
- (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.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonPressed:(id)sender {
[self.delegate addItem:self data:#"this is data"];
}
#end
and here is ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
#interface ViewControllerB : UIViewController<viewControllerADelegate>
#end
and ViewControllerB.m
#import "ViewControllerB.h"
#import "ViewControllerA.h"
#interface ViewControllerB ()
#end
#implementation ViewControllerB
- (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.
ViewControllerA *vba = [[ViewControllerA alloc]init];
[vba setDelegate:self];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item{
NSLog(#"delegate function called");
}
#end
The implemented function -(void) addItem: (ViewControllerA *)controller data:(NSString *)item is never called.Am I missing something ? Thanks in advance
Are you using storyboards? If yes, you just push the wrong button: storyboard initializes view controllers on its own, so yours code:
ViewControllerA *vba = [[ViewControllerA alloc]init];
[vba setDelegate:self];
just setting delegate on some unused view controller.
Use segues, don't try to reinvent a wheel.