custom View for universal application - iphone

I believe, that this question is duplicated, but i can't find it =(.
How to create own UIView class, which is loaded from (iPhone/iPad)*.xib
I was trying next things:
#interface CurtainView : UIView
...
- (id)init {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self = [[[NSBundle mainBundle] loadNibNamed:#"CurtainView_iphone" owner:self options:nil] objectAtIndex:0];
[self setFrame:CGRectMake(0, 0, 320, 460)];
}
else {
self = [[[NSBundle mainBundle] loadNibNamed:#"CurtainView_ipad" owner:self options:nil] objectAtIndex:0];
[self setFrame:CGRectMake(0, 0, 768, 1004)];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"there should be some animation on view appirance");
}
and ...
CurtainView* curtain = [[CurtainView alloc] init];
NSLog(#"before");
[self.view addSubview:curtain];
[curtain drawRect:CGRectMake(0, 0, 320, 460)];
But in this case, I haven't the result which I'm expecting, and drawRect isn't calling. I hope there is easy way to create custom Views for universal Apps.

put this code in .h file
#import
#interface CurtainView : UIView
#end
put this code in .m file
#import "CurtainView.h"
#implementation CurtainView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect{
// Drawing code
NSLog(#"there should be some animation on view appirance");
}
now call this UIView form your viewcontroller class. this will call drawRect
CurtainView* curtain = [[CurtainView alloc] init];
NSLog(#"before");
[curtain drawRect:self.view.frame];
[self.view addSubview:curtain];
you can call your viewcontroller class
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}

ok. sorry, I got my misstake:
it's need to use :
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:#"Curtain_iPhone" owner:self options:nil] objectAtIndex:0]];
instead my:
self = [[[NSBundle mainBundle] loadNibNamed:#"CurtainView_iphone" owner:self options:nil] objectAtIndex:0];
Hope it will helpful for some one.
moreover, I don't know why, but property "opaque" for the View should be set in "init" method, not only in IB.

Related

scrollview add other page xib's views

i have two .xib file. First xib name is viewcontroller.xib , it contain scrollview using page control.
second xib name is registration.xib , it contain labels textfield and submit button .
my question is that iwant to add the whole views of registration.xib file into the scrollview of viewcontroller.xib file .
what can i do ???????????
//In viewdidload of viewcontroller.m
regiViewController=[[RegistrationViewController alloc]init];
regiViewController.view11.translatesAutoresizingMaskIntoConstraints=NO;
UIView *containerView=[[UIView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
containerView.backgroundColor = [UIColor purpleColor];
[containerView addSubview:regiViewController.view11];
[scrollView addSubview:containerView];
In registration.m
- (void)setup {
[[NSBundle mainBundle] loadNibNamed:#"RegistrationViewController" owner:self options:nil];
[view11 addSubview:self.view];
self.view11.translatesAutoresizingMaskIntoConstraints=NO;
}
Personally I would use
[[RegistrationViewController alloc] initWithNibName:#"RegistrationViewController" bundle:nil];
rather than loading the XIB from the [setup] method but that's just me. However, in your current code viewcontroller.m never calls the setup method, so the XIB is never loaded. Try this:
//In viewdidload of viewcontroller.m
regiViewController=[[RegistrationViewController alloc]init];
[regiViewController setup];
UIView *containerView=[[UIView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
containerView.backgroundColor = [UIColor purpleColor];
[containerView addSubview:regiViewController.view11];
[scrollView addSubview:containerView];
Thank you dear for reply me but your code not working ...My setup method is called after this line
regiViewController=[[RegistrationViewController alloc]init];
// see Again viewcontroller.m
regiViewController=[[RegistrationViewController alloc]init];
regiViewController.view11.translatesAutoresizingMaskIntoConstraints=NO;
UIView *containerView=[[UIView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
containerView.backgroundColor = [UIColor purpleColor];
[containerView addSubview:regiViewController.view11];
[scrollView addSubview:containerView];
//registrationviewcontroller.m
#import "RegistrationViewController.h"
#interface RegistrationViewController ()
#end
#implementation RegistrationViewController
#synthesize view11;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self setup];
}
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.
}
- (void)setup {
[[NSBundle mainBundle] loadNibNamed:#"RegistrationViewController" owner:self options:nil];
[view11 addSubview:self.view];
self.view11.translatesAutoresizingMaskIntoConstraints=NO;
}
#end

iOS memory leaks causing crash in changing view controllers

I have created two view controllers in which there is UIImage animations.Its crashing frequently and showing memory leaks in xcode instrument.
My Controllers Code-
- (void)viewDidLoad {
NSArray *firstArray;
firstArray = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"up0001" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"up0002" ofType:#"png"]],
::
::
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"up0035" ofType:#"png"]], nil];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
imgView = UIViewContentModeScaleToFill;
[imgView setAnimationImages:firstArray];
imgView.animationDuration = 1.75;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
[self.view addSubview: imgView];
}
- (void)dealloc {
[super dealloc];
[imgView release];
imgView = nil;
}
And I am changing viewcontrollers as my rootviewcontroller by getting appdelegate object and calling following appdelegate function in my Appdelegate.m(Please suggest any good approach)
- (void)changeRootViewController:(NSString *)controllerName
{
if(self.viewController){
[self.viewController.view removeFromSuperview];
self.viewController=nil;
}
if (controllerName == #"ViewController") {
ViewController *lviewController =[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.viewController = (RootViewController *)lviewController;
[lviewController release];
lviewController.view = nil;
[self.window setRootViewController:self.viewController]; //LEAKS 100%
} else if (controllerName == #"MainViewController") {
// Use a different VC as roowViewController
MainViewController *lviewController =[[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.viewController = (RootViewController *)lviewController;
[lviewController release];
lviewController.view = nil;
[self.window setRootViewController:self.viewController]; //LEAKS 100%
} else if (controllerName == #"SecondViewController") {
// Use a different VC as roowViewController
SecondViewController *lviewController =[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.viewController = (RootViewController *)lviewController;
[lviewController release];
lviewController.view = nil;
[self.window setRootViewController:self.viewController]; //LEAKS 100%
}
[self.window makeKeyAndVisible];
}
And calling this in my respective controlers button pressed as -
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:#"ViewController"];
OR
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:#"MainViewController"];
I want to manage view controllers from my main controller swapping controllers in and out without Navigationbar.Please help me figure out the best possible approach and avoid leaks as well.
Look at this piece of code:
if (controllerName == #"ViewController") {
ViewController *lviewController =[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.viewController = (RootViewController *)lviewController;
[lviewController release];
lviewController.view = nil;
[self.window setRootViewController:self.viewController]; //LEAKS 100%
}
When you alloc "lviewController", its retain count is 1;
when you do "self.viewController" as I suppose viewController is a retained property, then lviewController retain count is increased to 2;
then you release it, balancing the previous alloc, and retain count returns to 1;
finally you assign it to rootViewController, which is a retained property, so again lviewController retain count is 2;
finally when you "swap" view controller in your window, the lviewController is released, so it retain count becomes 1. As you can see, it will never be dealloc'd. This means that each time you call this function, you make a leak.
You require to change some of your statement in your code ..
This line have no meaning ..
imgView = UIViewContentModeScaleToFill;
There is a function by using that you have to set the value of the UIImageView property to UIViewContentModeScaleToFill find that in a UIImageView documentation.
[imageView setContentMode:UIViewContentModeScaleToFill];
The same with this...
lviewController.view = nil; //Remove this from your code ..
And finally change the implmentation for the dealloc function and remember [super dealloc]; should be in the last in any implementation of dealloc .
- (void)dealloc {
[imgView release];
imgView = nil;
[super dealloc];
}
The probable leak is due to the code inside "SecondViewController", "MainViewController", "ViewController" classes. And the reason for crash is you are releasing image view after [super dealloc]
try this,
- (void)dealloc {
[imgView release];
imgView = nil;
[super dealloc];
}

iPhone/Objective-c - Gaining access to a previous ViewController from a subview?

How do I access a previous view controller while in a subview? Because I'm able to perform actions but I'm just not able to use self.[my main view controller].
This is my code, for testing purposes:
PhotoViewController.m
-(IBAction)likeButton:(UIButton *)sender
{
//this part works
NSString *num = #"2";
self.label.text = [NSString stringWithFormat:#"%# + %#",
self.label.text,
num];
//this part doesn't work
//switch over to the third view to see if it worked
self.tabBarController.selectedIndex = 0;
}
I have a UITabBarController and one of its view controllers has a UIScrollView. Inside of the UIScrollView is a PhotoViewController object.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch
MyTabBarViewController *vc2 = [[MyTabBarViewController alloc] init];
SecondViewController *vc3 = [[SecondViewController alloc] init];
controller = [[DemoAppViewController alloc] init];
controller.view.frame = CGRectMake(0, 20, 320, 460);
controller.title = #"Intro Screen";
vc2.title = #"Explore";
vc3.title = #"Send a Pic";
UITabBarController *tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:controller, vc2, vc3, nil];
[controller release];
[vc2 release];
[vc3 release];
[self.window addSubview:tbc.view];
[self.window makeKeyAndVisible];
return YES;
}
Also, my TabBarViewController.m //not my actual UITabBarController though, wording is confusing
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
arrayCount = [array count];
scroller.delegate=self;
scroller.pagingEnabled=YES;
scroller.directionalLockEnabled=YES;
scroller.showsHorizontalScrollIndicator=NO;
scroller.showsVerticalScrollIndicator=NO;
//should have an array of photo objects and the number of objects, correct?
scrollWidth = 0;
scroller.contentSize=CGSizeMake(arrayCount*scroller.frame.size.width, scroller.frame.size.height);
for (int i = 0; i < arrayCount;i++) {
PhotoViewController *pvc = [[PhotoViewController alloc] initWithNibName:#"PhotoViewController" bundle:nil];
UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:CGRectOffset(scroller.bounds, scrollWidth, 0)];
CGRect rect = scrollImageView.frame;
pvc.view.frame = rect;
pvc.label.textColor = [UIColor whiteColor];
id individualPhoto = [array objectAtIndex:i];
NSLog(#"%#",individualPhoto);
NSArray *keys=[individualPhoto allKeys];
NSLog(#"%#",keys);
NSString *imageURL=[individualPhoto objectForKey:#"source"];
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
pvc.imageView.image = [[UIImage alloc] initWithData:data];
pvc.label.text = [individualPhoto objectForKey:#"id"];
[scroller addSubview:pvc.view];
[scrollImageView release];
//[pvc release];
scrollWidth += scroller.frame.size.width;
}
if (arrayCount > 3) {
pageControl.numberOfPages=3;
} else {
pageControl.numberOfPages=arrayCount;
}
pageControl.currentPage=0;
}
If a UIView subclass needs to message a controller above itself, a common (and often recommended?) way to do this is to have the view subclass implement the delegate protocol (a weak linked instance variable pointing to the view controller that you want to use). The view controller should then set itself as the delegate of the view when that view is being initialize.
Try defining this method in your PhotoViewController:
+ (YOURTABBARCONTROLLER*)parentTabBarController:(UIResponder*)view {
id nextResponder = nil;
id v = view;
while (nextResponder = [v nextResponder]) {
NSLog(#"Found Responder: %#", nextResponder); //-- ADDED THIS
if ([nextResponder isKindOfClass:[YOURTABBARCONTROLLER class]])
return nextResponder;
v = nextResponder;
}
return nil;
}
it will traverse the responder chain and return the first controller of a given type that is found. Replace YOURTABBARCONTROLLER with your actual tab bar controller class and you should be able to have:
-(IBAction)likeButton:(UIButton *)sender
{
//this part works
NSString *num = #"2";
self.label.text = [NSString stringWithFormat:#"%# + %#",
self.label.text,
num];
[PhotoViewController parentTabBarController:self.view].selectedIndex = 0;
// self.tabBarController.selectedIndex = 0;
}
Updated
-(IBAction)likeCommentButton:(UIButton *)sender
{
//code goes here
TypeSomethingViewController *typeSomethingViewController = [[TypeSomethingViewController alloc] init];
typeSomethingViewController.delegate = self;
[self presentModalViewController:typeSomethingViewController animated:YES];
[typeSomethingViewController release];
}
-(void)typeSomethingViewController:(TypeSomethingViewController *)controller didTypeSomething:(NSString *)text
{
//NSLog(#"response: %#", controller);
NSString *commentID = self.label.text;
for(UIViewController *controller in [PhotoViewController parentTabBarController:self.parentViewController.view].viewControllers)
{
if([controller isKindOfClass:[DemoAppViewController class]])
{
DemoAppViewController *davc = (DemoAppViewController *)controller;
//[davc commentPicture:commentID :message];
[davc likePicture:commentID];
}
}
[PhotoViewController parentTabBarController:self.view].selectedIndex = 0;
[self dismissModalViewControllerAnimated:YES];
}
Try this out to switch to the first controller.
self.tabBarController.selectedViewController
= [self.tabBarController.viewControllers objectAtIndex:0];
Change the index value to switch to whichever controller you want to switch to.
The tabBarController should be a property of the app delegate.
Try accessing it with :
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication]
delegate];
appDelegate.tabBarController.selectedIndex = 0;
Unless you declare the same property in PhotoViewController, you won't be able to access it this way.
Try importing the app delegate header in PhotoViewController.h like so :
#import "MyAppDelegate"
then try this code above replacing tabBarController by tbc which, as the example suggests is the name given to this property.

UIView not being positioned with setFrame

I'm probably not doing this correctly. Having not done an iPad app before I'm doing what seems natural to me.
I have a UIView that is going to be made of up other UIViews. Specifically, I want to dynamically create and add subviews to each of these subviews. In other words:
UIView
|
| --> UIView
|
| --> Dynamic UIView 1
| --> Dynamic UIView 2
| --> ...
I managed to get 'Dynamic UIView 1' by adding to its initWithFrame this bit of code to load the xib:
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
[self addSubview:infoView];
It works! The problem I'm having is that 'Dynamic UIView n' is always rendered at the top left corner of the screen. If I try to change the position with setFrame I get a black box at representing the frame however the actual control content remains in the top left.
Any ideas? Is there a better way to do this?
----- Update -----
Here is a screenshot of the root view.
Overview of main UIView http://www.freeimagehosting.net/uploads/460090996d.png
The entire control is call CurrentItemsViewController. The green area is a UIView within that I have linked back in code via a link in the IB, in the code sample it is called "itemsUIView". Here is how I am adding items within CurrentItemsViewController.m
- (id)init {
[super initWithNibName:nil
bundle:nil];
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"Current"];
/* Approach 1 */
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *subItemView1 = [nibViews objectAtIndex:0];
/* Approach 2 - this apprach does Approach 1 in the initWithFrame method */
//ItemView *subItemView1 = [[ItemView alloc] initWithFrame:CGRectMake(120, 120, 354, 229)];
/* Approach 3 */
//ItemViewController *ctr = [[ItemViewController alloc] init];
//UIView *subItemView1 = [ctr view];
[[self view] addSubview:subItemView1];
// [[self itemsUIView] addSubview:subItemView1]; <-- doesn't render subItemView1 at all
[subItemView1 release];
[subItemView1 setFrame:CGRectMake(120, 120, 354, 229)];
[subItemView1 setBounds:CGRectMake(120, 120, 354, 229)];
return self;
}
Inside of ItemView.m I have this, it only gets call by Approach 2 and 3.
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
[self addSubview:infoView];
}
return self;
}
As shown, I tried creating the ItemView 3 different ways and each one renders the same result. When I add the view as a subview of the root view I get this. Notice that a square appears where it should render, however ViewItem actually renders at the top left corner. In the sample above, when I add subItemView1 it doesn't render anything at all.
alt text http://www.freeimagehosting.net/uploads/c6635ce860.png
Try setting the frame before you add it as a subview.
Your code:
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
self addSubview:infoView];
Should be:
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:#"ItemView"
owner:[[ItemViewController alloc] init]
options:nil];
ItemView *infoView = [nibViews objectAtIndex:0];
infoView.frame = CGRectMake( put your rect here );
[self addSubview:infoView];

How to change background image of a button and get it updated instantly?

Im developing a memory game. As i click on a button I want the background to switch. When I click on the card the second time i want the background to switch for a second and then go back to the original (if it wasnt the correct card). But the background of the buttons are not changed until i return from the method (buttonPressed). Why isnt it changed instantly? Please help me! :)
Here is the class that i am working with:
#import "Button.h"
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#class ZogajAppDelegate;
#interface PairPlayPlayViewController : UIViewController <AVAudioPlayerDelegate> {
ZogajAppDelegate *appDelegate;
AVAudioPlayer *av;
Boolean isPairActive;
Button *firstButton;
Button *secondButton;
}
#property (nonatomic, retain) AVAudioPlayer *av;
#property (nonatomic, retain) Button *firstButton;
#property (nonatomic, retain) Button *secondButton;
- (void)buttonPressed:(id)sender;
- (void)secondCard;
#end
and the .m file:
#import "PairPlayPlayViewController.h"
#import "ZogajAppDelegate.h"
#import "Button.h"
#import "AVFoundation/AVAudioPlayer.h"
#implementation PairPlayPlayViewController
#synthesize av, firstButton, secondButton;
/*
// 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];
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:#"double_card" ofType:#"mp3"];
NSString *newAudioFile2 = [[NSBundle mainBundle] pathForResource:#"right_sound" ofType:#"mp3"];
av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
[av prepareToPlay];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile2] error:NULL];
[av prepareToPlay];
//av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
//[av prepareToPlay];
isPairActive = FALSE;
appDelegate = (ZogajAppDelegate *)[[UIApplication sharedApplication] delegate];
int position;
for(int i = 0; i < ((appDelegate.numberOfPairs*2)/5); i++){
for (int j = 0; j<5 ;j++){
Button *button = [[Button alloc] init];
//Sätter bild nummer..
position = (arc4random() % [appDelegate.cardsInPlay count]);
button.picNumber = [[appDelegate.cardsInPlay objectAtIndex:position]intValue];
NSLog(#"%d", button.picNumber);
[appDelegate.cardsInPlay removeObjectAtIndex:position];
button.frame = CGRectMake( 3 + (65 * j), 5 + (65 * i), 55, 55);
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.clipsToBounds = YES;
[button setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
for (int j = 0; j<((appDelegate.numberOfPairs*2) % 5) ;j++){
Button *button = [[Button alloc] init];
//Sätter bild nummer..
position = (arc4random() % [appDelegate.cardsInPlay count]);
button.picNumber = [[appDelegate.cardsInPlay objectAtIndex:position]intValue];
NSLog(#"%d", button.picNumber);
[appDelegate.cardsInPlay removeObjectAtIndex:position];
button.frame = CGRectMake( 3 + (65 * j), 5 + (65 * ((appDelegate.numberOfPairs*2)/5) + 1), 55, 55);
//button.imageView.contentMode = UIViewContentModeScaleAspectFit;
//button.imageView.clipsToBounds = YES;
[button setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:)
forControlEvents:UIControlEventAllTouchEvents];
[self.view addSubview:button];
}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
-(void)buttonPressed:(id)sender {
Button *button = [[Button alloc] init];
button = sender;
NSLog(#"%d och spela upp ljud", button.picNumber);
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:#"double_card" ofType:#"mp3"];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
[av play];
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",button.picNumber]] forState:UIControlStateNormal];
[self.view addSubview:button];
[NSThread sleepForTimeInterval:1.0];
if(isPairActive == FALSE){
isPairActive = TRUE;
firstButton = button;
}
else {
secondButton = button;
[self secondCard];
}
return;
}
- (void)secondCard {
[NSThread sleepForTimeInterval:1.0];
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:#"double_card" ofType:#"mp3"];
isPairActive = FALSE;
if([appDelegate.dictionary objectForKey:[NSString stringWithFormat:#"%d",secondButton.picNumber]] == [NSString stringWithFormat:#"%d",firstButton.picNumber]){
NSString *newAudioFile2 = [[NSBundle mainBundle] pathForResource:#"right_sound" ofType:#"mp3"];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile2] error:NULL];
[av play];
}
else {
[firstButton setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[secondButton setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[av initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
[av play];
}
return;
}
- (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 {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[av release];
[firstButton release];
[secondButton release];
}
#end
In buttonPressed: change the background, set an NSTimer to call you back, and return. In the NSTimer callback, set the background back to what you want.
Drawing doesn't happen at the moment you ask for it. That would give you horrible performance. All drawing is merged into the draw portion of the run loop. You won't get to that portion until you return from this IBAction method.
As you already know, the image will be changed after returning from the method. That's it.
You have to return from the method, so you have to change the image, perform selector that executes the rest of code you want to perform and return from the method.
UIKit can run only on main thread, so you have to return from the method, that's blocking it, and it will execute GUI manipulations then.
EDIT:
Instead of telling NSThread to sleep, perform the selector with delay.