I have an App that I am making thatis similar to Urbanspoon. I have a picker the spins and it is hooked up to an IBAction for a UIButton. I want a mp3 file to play once the button is pressed. I feel it will give the user more of an experience by doing this.
I imported the AVFoundationFramework and the AudioToolBoxFramework and imported their .h into my main .m file
Here is my action.
- (IBAction)spinTheSpinner:(id)sender {
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"GoldTapButton.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else {
[sender setImage:[UIImage imageNamed:#"PurpleTapButton.png"] forState:UIControlStateHighlighted];
[sender setSelected:YES];}
[picker1 selectRow:(arc4random() % [self pickerView:picker1 numberOfRowsInComponent:0]) inComponent:0 animated:YES];
}
Do i need to make a separate action for the same button or can i just add a block of code into this same action. I have the mp3 file is my resources folder so its in my project I just cant find where and what to do to make it happen.
Here is my .h as well just for good measure
#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController : UIViewController
- (IBAction)spinTheSpinner:(id)sender;
#property (nonatomic, retain) NSMutableArray *picker1Data;
#end
also here is the top of my .m file
#import "ViewController.h"
#import "Twitter/Twitter.h"
#import "UIKit/UIKit.h"
#import"AVFoundation/AVAudioPlayer.h"
#interface ViewController ()
{
#private
UIPickerView *picker1;
}
#end
#implementation ViewController
Any help or suggestion or code would be a huge help in any way. I am running on a bit of a time schedule. THANKS S.O
Yes you can just add the code into the same IBAction method. You re not limited to th amount of code you can run in the method its currently linked to
Related
EDIT:
In my ViewController i am adding multiple buttons to its view.
ViewController1.h
#import <UIKit/UIKit.h>
#interface ViewController1 : UIViewController
-(void)buttonTapped:(id)sender;
#end
ViewController1.m
#import "MyViewCreatorClass.h"
#interface ViewController1 ()
#end
#implementation ViewController1
- (void)viewDidLoad {
[self.view addSubView:[MyViewCreatorClass createButtonWithParentView:self]];
}
-(void)buttonTapped:(id)sender{
NSLog(#"tapped");
}
The implementation for creating the button is inside the class 'MyViewCreatorClass'
MyViewCreatorClass.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "MainViewController.h"
#interface MyViewCreatorClass : NSObject
+ (UIButton*)createButtonWithParentView:(ViewController1*)parentView;
#end
MyViewCreatorClass.m
#import "MyViewCreatorClass.h"
#implementation MyViewCreatorClass
+ (UIButton*)createButtonWithParentView:(ViewController1*)parentView {
UIButton *button = [[UIButton alloc] initWithFrame:....];
//some other implementation (title, color etc.)
[button addTarget:parentView action:(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
#end
At first i had this implementation inside ViewController1 and it worked fine, but now
the warning "undeclared selector 'buttonTapped:'" shows up (this was solved by declaring the method buttonTapped: in ViewController1.h)
the method won't get called as i moved this method into MyViewCreatorClass.
I know that parentView needs to be an instantiated object, and it is. In another method of MyViewCreatorClass i'm also setting some views delegate to parentView and it works.
I even tried to use this method as an instance method not a class method but it did not work either.
What could it be that prevents the method from being called?
Additional question:
One could also use protocols to achieve this (comments). I know how to use protocols, but sadly i don't know how to implement this so a button calls a method on touchUpInside.
Any ideas?
I am working on an iOS project and wanted to include a UIView that is reused on multiple screens in the application (appearing at the bottom of different UIViews). I am using a storyboard for the UI work so far but created an .xib file to be used by the reusable view (playerView in code below).
The view gets added but the button I have added to the View is unresponsive, also my background color cannot be changed on the view. I have tried to set background color programmatically and in the .xib with no luck. Very weird symptoms and I tried to instantiate the view from #5 of this article and probably did something wrong. I dont fully understand everything in the article which makes me nervous - for instance my showSubclassedView method returns IBAction but I just call the function name in code and dont use a button (I did hook up the buttons in the view as the article described though).
Here is the code:
EventViewController.m (where I trry and add PlayerView)
#import "EventViewController.h"
#import "PlayerView.h"
#interface EventViewController ()
-(IBAction)showSubclassedView;
#end
#implementation EventViewController
-(IBAction)showSubclassedView
{
[PlayerView presentInViewController:self];
}
PlayerView.h (.h for reusable view)
#import <UIKit/UIKit.h>
#import "Utils.h"
#class PlayerView;
#protocol PlayerViewDelegate
-(void)playerViewTouchedUp:(PlayerView*) playerView;
-(void)playerViewDidDismiss:(PlayerView*) playerView;
#end
#interface PlayerView : UIView
+(void)presentInViewController:(UIViewController<PlayerViewDelegate>*) playerView;
-(IBAction)viewTouchedUp;
-(IBAction)dismiss;
#end
PlayerView.m (.m for reusable view)
#import "PlayerView.h"
#interface PlayerViewOwner : NSObject
#property(nonatomic,weak) IBOutlet PlayerView *playerView;
#end
#implementation PlayerViewOwner
#end
#interface PlayerView ()
#property (nonatomic, weak) UIViewController <PlayerViewDelegate> *delegateViewController;
#end
#implementation PlayerView
+(void)presentInViewController:(UIViewController<PlayerViewDelegate> *)viewController
{
PlayerViewOwner *owner = [PlayerViewOwner new];
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:owner options:nil];
owner.playerView.delegateViewController = viewController;
[viewController.view addSubview:owner.playerView];
}
-(IBAction)viewTouchedUp
{
//forward to delegate
NSLog(#"you clicked a button");
[self.delegateViewController playerViewTouchedUp:self];
}
-(IBAction)dismiss
{
[self removeFromSuperview];
// Forward to delegate
[self.delegateViewController playerViewDidDismiss:self];
}
#end
PlayerView.xib has a UIbutton on it that connects it to viewTouchedUp method in PLayerView.m
Is there anything I did wrong in the code above? Is this the best way to do a reusable view to display on other views?
Thank you!
Here's a workaround I found after not being able to solve this.
New implementation is from this article
I reverted my changes from my initial question and implemented this. It lacks the nice protocol separation for talking back and forth and I might need something like this later but I think now I can still implement a protocol to solve this.
At least with this solution I have usable items in my view and a background that changes!
It is one of my first days at iOS programming. I came from C++ and have specific silly question. How does compiler work and go through the code?
How I understand all starts with public interface, then continues to private. How implementation works? Methods? Is view controller as main function in C++ and it goes through all methods at the start?
Here is my viewController .h and .m. This program already has some other classes and action buttons. Maybe someone can explain step-by-step. Thanks.
ViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#import "Deck.h"
#import "PlayingCardDeck.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
#property (nonatomic) int flipCount;
#property (strong, nonatomic) Deck *deck;
#end
#implementation ViewController
- (Deck *)lol
{
if (!_deck) _deck = [self createDeck];
return _deck;
}
- (Deck *)createDeck
{
return [[PlayingCardDeck alloc] init];
}
- (void)setFlipCount:(int)flipCount{
_flipCount=flipCount;
_flipsLabel.text = [NSString stringWithFormat:#"Count: %d", _flipCount];
NSLog(#"%d", self.flipCount);
}
- (IBAction)touchCardButton:(UIButton *)sender {
if ([sender.currentTitle length]) {
[sender setBackgroundImage:[UIImage imageNamed:#"cardback"] forState:UIControlStateNormal];
[sender setTitle:#"" forState:UIControlStateNormal];
}
else {
Card *randomCard = [self.lol drawRandomCard];
if (randomCard){
[sender setBackgroundImage:[UIImage imageNamed:#"cardfront"] forState:(UIControlStateNormal)];
[sender setTitle:randomCard.contents forState:UIControlStateNormal];
self.flipCount++;
}
}
}
#end
The beginning of the program code is similar to c++, it is the very c main() function.
Within your main the framework is called (by instaticating an UIApplication or subclass object) and the names of the application class and the application delegate class are handed over to the framework. The cocoa touch framework will then instantiate the application first and instantiate the delegate and assign the delegate to the application.
Typcially the first method from your program code that is invoked is the UIApplicationDelegate method application:willFinishLaunchingWithOptions: but the one that you typcially start with is application:didFinishLaunchingWithOptions:
There some preparation is set up, which is often not required especially when you work with a storyboard or when you set the main interface in your project properties. According to these properties or storyboard one view controller is the root view controller that is invoked first. For the view controllers it is quite similar. You would use NIB files or storyboards in most cases to define their UI Items and then viewDidLoad is the first method that is being invoked with regard to the root view.
Given your code you are likely to add something like this:
-(void) viewDidLoad {
[super viewDidLoad];
[self createDeck]
// here you may want to access some UIView objects in order to display your deck.
}
I don't know if you understand how c++ compiles, Objective-C is all the same.
First preprocessor translate all preprocess code(#import, #macro, etc), and then every .m file is considered a compile unit, they are compile separately in to object files without a specific order.
Then linker links all object files together.
If you don't understand what compile unit or object file are, I suggest you do more research on how preprocessor, compiler, linker works, since it is a too broad topic to explain here.
I am trying to changing the visual aspects of my button, however I am trying to use the FlatUIKit's button aspect, for those who don't know what that is that ok as it doesn't matter, in the example project that they provide they change the color of an IBOutlet button however it dosen't work the same with an IBAction heres their example code:
#import "ViewController.h"
#import "FUIButton.h"
#import "UIColor+FlatUI.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet FUIButton *alertViewButton;
//as you can see its a FUIButton, and I'm not sure how to use that with an IBAction
#end
#implementation ViewController
- (void)viewDidLoad
{
self.alertViewButton.buttonColor = [UIColor peterRiverColor];
self.alertViewButton.shadowColor = [UIColor silverColor];
self.alertViewButton.shadowHeight = 3.0f;
self.alertViewButton.cornerRadius = 6.0f;
self.alertViewButton.titleLabel.font = [UIFont boldFlatFontOfSize:16];
[self.alertViewButton setTitleColor:[UIColor cloudsColor] forState:UIControlStateNormal];
[self.alertViewButton setTitleColor:[UIColor cloudsColor] forState:UIControlStateHighlighted];
}
Heres my code this far:
my .h
#import <UIKit/UIKit.h>
#interface HomeViewController : UIViewController {
}
- (IBAction)start:(id)sender;
#end
my .m:
#import "HomeViewController.h"
#import "UIColor+FlatUI.h"
#import "FUIButton.h"
#interface HomeViewController ()
#end
#implementation HomeViewController
- (IBAction)start:(id)sender {
//this is the button i want change the appearance of.
}
I am stuck with this, the button I want to use the FUIButton color effects is the IBAction called start, any help? Thanks in advance.
Note: I am using storyboards if that makes a difference.
I have a mistake with change the text to uibutton in iOS
#interface ViewControllerSonidos : UIViewController{
__weak IBOutlet UIButton *initgame;
}
#property (weak, nonatomic) IBOutlet UIButton *initgame;
- (IBAction)Reproducir:(id)sender;
And my implementation file looks like this:
#implementation ViewControllerSonidos
#synthesize initgame
- (IBAction)Reproducir:(id)sender {
[initgame setTitle:#"Continue" forState:UIControlStateNormal];
}
but the problem is that the text is never changed, somebody looks what is wrong?
Thanks in advance!
You have many ways you can do this.
You can just connect it with the IBOutlet,
you can use it with id sender
or you could just use the simple IBAction signature
examples:
// using the id sender method
- (IBAction) buttonTitleChange:(id)sender {
[sender setTitle: #"Title" forState:UIControlStateNormal];
}
// using the connection one
- (IBAction) buttonTitleChange: (id)sender {
//AFTER CONNECTING THE BUTTON IN .XIB
[buttonName setTitle:#"Title" forState:UIControlStateNormal];
}
// and the regular void method....also requiring the linkage of the .xib
- (IBAction) buttonTitleChange {
[buttonName setTitle:#"Title" forState:UIControlStateNormal];
}
just remember to connect the button in .xib for them all
using the id sender, just says that you can use it for any type object. the id sender with connection is just a way of writing the method, and the regular method is just a way of practice.
It all depends on how you program.
Hope this helps!
try this code:
- (IBAction)Reproducir:(id)sender {
[sender setTitle:#"Continue" forState:UIControlStateNormal];
}
May be problem with IBOutlet ,you forgot to connect it in XIB otherwise it'l works fine.
-(void)btnChanged
{
[m_btnSample setTitle:#"Continue" forState:UIControlStateNormal];
}
Yes please check IBOutlet connection, because same code worked for me.
in ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction)initgame:(id)sender;
#property (retain, nonatomic) IBOutlet UIButton *initgame;
#end
in ViewController.m
- (IBAction)initgame:(id)sender {
[self.initgame setTitle:#"Continue" forState:UIControlStateNormal];
}
don't forget to attach button from .xib to property and IBAction.
if It doesn't work now, just remove the connection between the Button and IBAction/Property, create new IBAction and property for it and it will work for the above code.