Hi I was trying some examples and checking documentation but I can't get this to work.
Here's my code:
#interface AHMSoudView : UIView <UIInputViewAudioFeedback>
- (void) playClick;
#end
#implementation AHMSoudView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (BOOL) enableInputClicksWhenVisible {
return YES;
}
- (void) playClick {
[[UIDevice currentDevice] playInputClick];
}
#end
In the storyboard I have the view set to my AHMSoundView. In the controller I then have:
- (IBAction)keypadTouchDown:(id)sender {
[((AHMSoudView *)self.view) playClick];
}
But nothing happens. What am I doing wrong?
Related
I have a custom UIWebView (EpubWebView), with a custom NSURLCache (EpubCache) for handling requests.
i created a custom delegate for handling request.
EpubCache.h
#protocol EpubCacheDelegate <NSObject>
#required
- (NSCachedURLResponse *)hadleRequest:(NSURLRequest *)request;
#end
#interface EpubCache : NSURLCache
#property (nonatomic, weak) id <EpubCacheDelegate> cacheDelegate;
#end
EpubCache.m
import "EpubCache.h"
#interface EpubCache ()
#end
#implementation EpubCache
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
return [self.cacheDelegate hadleRequest:request];
}
#end
EpubWebView .h
#interface EpubWebView : UIWebView <UIWebViewDelegate, EpubCacheDelegate>
#property (strong, nonatomic) EpubCache *mLocalCache;
#end
EpubWebView.m
- (void) localInit
{
self.mLocalCache = [[EpubCache alloc] init];
self.mLocalCache.cacheDelegate = self;
[NSURLCache setSharedURLCache:self.mLocalCache];
}
- (NSCachedURLResponse *)hadleRequest:(NSURLRequest *)request
{
// return handled request
}
on the other hand i have a navigationcontroller with a tableview and the destination view controller have this webview.
when i ran the app and click on an item in tableview, everything is fine and delegate works as expected.
if i click back and click on other item in tableview, things goes wrong, the cachedResponseForRequest getting called but the hadleRequest wont, i checked and findout that the delegate is null!
i can not figure out what is happening here.
any help would be appreciated.
UPDATE 1
EpubWebView.m
- (id)init
{
self = [super init];
if (self)
{
[self localInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self localInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
[self localInit];
}
return self;
}
UPDATE 2
the segue of the tableview that bring up the view controller that contain EpubWebView
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
BookViewController *bookController = segue.destinationViewController;
bookController.mBook = booksList[indexPath.row];
}
and BookViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
mWebView = [[EpubWebView alloc] initWithFrame:self.mainView.frame];
[self.mainView addSubView:mWebView];
[mWebView setBook:self.mBook];
}
You need to make a few modifications. First, remove your "localInit" method, then create a new function in EPubWebView:
- (void) setCache: (EpubCache *)localCache
{
localCache.cacheDelegate = self;
}
Now, you can create and hold onto your cache in your BookViewController via these lines in the .m file:
#interface BookViewController ()
#property (strong, nonatomic) EpubCache *mLocalCache;
#end
and change your BookViewController's viewDidLoad method to look like:
- (void) viewDidLoad {
self.mLocalCache = [[EpubCache alloc] init];
// only need to do this once, at viewDidLoad time
[NSURLCache setSharedURLCache:self.mLocalCache ];
[super viewDidLoad];
mWebView = [[EpubWebView alloc] initWithFrame:self.mainView.frame];
[mWebView setCache:self.mLocalCache];
[self.mainView addSubView:mWebView];
[mWebView setBook:self.mBook];
}
I can't disable screen rotation with:
- (BOOL)shouldAutorotate {
return NO;
}
screen rotation will disable only if I disabled it from the general info of the project
but I don't wont to disable it from there, this is my code (not a lot):
#import "MainPage.h"
#import "Reachability.h"
#interface MainPage ()
#end
#implementation MainPage
Reachability *internetReachableFoo;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidLoad];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
background.frame = CGRectMake(0, 0, 320, 480);
about.frame = CGRectMake(20, 401, 280, 44);
}
}
NSUserDefaults *userdetails =[NSUserDefaults standardUserDefaults];
NSString *userid = [userdetails objectForKey:#"userid"];
NSString *username = [userdetails objectForKey:#"username"];
if (userid != NULL && username != NULL) {
[self performSegueWithIdentifier:#"Home" sender:self];
NSLog(#"connceted");
}
else
{
signOutlet.hidden = NO;
loginOutlet.hidden = NO;
}
}
- (BOOL)shouldAutorotate {
return NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am using IOS 8 and IOS 7 any idea ?
* edit *
ok, I don't get it in other VC its work and can't rotate
this is my other VC (I don't see any change)
#import "About.h"
#interface About ()
#end
#implementation About
- (void)viewDidLoad {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
background.frame = CGRectMake(0, 0, 320, 480);
aboutText.frame = CGRectMake(15, 68, 290, 319);
home.frame = CGRectMake(67, 414, 187, 38);
}
}
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate {
return NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You need to include the supportedInterfaceOrientations method in your view controller along with the shouldAutorotate method. So lets say you want this one specific view controller limited to only the Portrait orientation, you would have:
#import "MyViewController.h"
#interface MyViewController ()
#end
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
//...
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
//...
}
- (NSUInteger)supportedInterfaceOrientations {
// support only portrait landscape
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL) shouldAutorotate {
// have autorotate set to YES so the screen can rotate if not in Portrait orientation
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
// not necessary, but this makes the view start in Portrait orientation on the initial load
return UIInterfaceOrientationPortrait;
}
#end
Hope this helps
I've created a gradient using Quartz, only one issue. I can't get it to rotate with the screen. I've tried everything listed on this tutorial. Here's the view controller header:
#import <UIKit/UIKit.h>
#interface RadialGradientBackgroundViewController : UIViewController {
}
#end
And here's the view controller:
#import "RadialGradientBackgroundViewController.h"
#import "BackgroundLayer.h"
#implementation RadialGradientBackgroundViewController
/*
// 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 {
}
*/
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CAGradientLayer *bgLayer = [BackgroundLayer gradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[[[self.subview.layer sublayers] objectAtIndex:0] setFrame:self.view.bounds]; // Error is here!
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
Your view controller does not have a subview property or method. As rmaddy said, change subview to view.
I have posted a question here . In which one buddy replied and gave solution. Although the solution is working on few view controllers but on view its not working. When I enter a view controller that has TabController + navigation controller on there tab Bar Items, the code doesn't work. and the views are able to rotate.
I used the Following code for iOS 6.1
//For iOS6
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
I have to implement the same thing in iOS 7 too.
Please Help
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
The above methods don't get called of a viewcontroller if they are inside any tabbarcontroller of navigationcontroller. If these methods are declared inside tabbarcontroller or navigation controller then they will get called.
For solving this make a class FixedOrientationTab, it is a subclass of UITabBarController, and a navigation class OrientationEnabledNavigation, it is a subclass of UINavigationController. Then implemented shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation methods inside FixedOrientationTab and OrientationEnabledNavigation.
OrientationEnabledNavigation.h
#import <UIKit/UIKit.h>
#interface OrientationEnabledNavigation : UINavigationController
#end
OrientationEnabledNavigation.m
#import "OrientationEnabledNavigation.h"
#interface OrientationEnabledNavigation ()
#end
#implementation OrientationEnabledNavigation
- (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.
}
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
FixedOrientationTab.h
#import <UIKit/UIKit.h>
#interface FixedOrientationTab : UITabBarController
#end
FixedOrientationTab.m
#import "FixedOrientationTab.h"
#interface FixedOrientationTab ()
#end
#implementation FixedOrientationTab
- (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.
}
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Then if you want to use navigation controller in your project then use OrientationEnabledNavigation and for tabbar FixedOrientationTab. After that if you implement shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation these method inside your viewcontroller then they will get called.
Hope this helps.. :)
Edit
As Jeev pointed out another way is that add some code beginning of your app delegate:
For UITabBarController
#implementation UITabBarController (AutoRotationForwarding)
-(BOOL)shouldAutorotate
{
if ([self.selectedViewController respondsToSelector:#selector(shouldAutorotate)]) {
return [self.selectedViewController shouldAutorotate];
}
else {
return YES;
}
}
- (NSUInteger)supportedInterfaceOrientations {
if ([self.selectedViewController respondsToSelector:#selector(supportedInterfaceOrientations)]) {
return [self.selectedViewController supportedInterfaceOrientations];
}
else {
return UIInterfaceOrientationMaskAll;
}
}
#end
For UINavigationController
#implementation UINavigationController (AutoRotationForwarding)
-(BOOL)shouldAutorotate
{
if ([self.topViewController respondsToSelector:#selector(shouldAutorotate)]) {
return [self.topViewController shouldAutorotate];
}
else {
return YES;
}
}
-(NSUInteger) supportedInterfaceOrientations {
if([self.topViewController respondsToSelector:#selector(supportedInterfaceOrientations)])
{
return [self.topViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskPortrait;
}
#end
I have UINavigationController contains UIViewController.
You should disable rotate in UINavigationController by subclass UINavigationController and implement
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
// return some
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
// return some thing that you want
}
Hope this helps you.
Since I m using a TabBarController, the only working solution I got was to create a category.
so just make a category
here's the .h class of the category, I named it AutoRotationtab
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface UITabBarController (AutoRotationTab)
#end
here's the .m class of the category
#import "UITabBarController+AutoRotationTab.h"
#implementation UITabBarController (AutoRotationTab)
-(BOOL)shouldAutorotate
{
if(globalvariable_shouldRotate)
{
return YES;
}
else
{
return NO;
}
}
- (NSUInteger)supportedInterfaceOrientations
{
if(globalvariable_shouldRotate)
{
return UIInterfaceOrientationMaskAll;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}
just make this category, and see the Magic, It works
I decided to try and make a Singleton for use with locations. I have got what I think to be, the singleton working correctly however I have one error that is now appearing. It tells me that my implementation is incomplete. It is the only error it is giving me and I am sure it is something wrong with either my view header or m file. I have tried a few things now and cannot get it to work. What am I missing here?
Here is my header
#import <UIKit/UIKit.h>
#interface TrackerViewController : UIViewController
-(void) locationUpdate;
end
And here is my implementation file:
#import "TrackerViewController.h"
#import "MyLocation.h"
#implementation TrackerViewController
NSString *LatCoord;
NSString *LongCoord;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[LocationController sharedInstance].locDelegate = (id)self;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[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];
}
- (void)locationUpdate:(CLLocation *)location {
[[LocationController sharedInstance] setLocation:location];
LatCoord = [NSString stringWithFormat:#"%lf", location.coordinate.latitude];
LongCoord = [NSString stringWithFormat:#"%lf", location.coordinate.longitude];
}
- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
-(IBAction)CheckIn:(id)sender
{
[self locationUpdate];
}
#end
My singleton header is as follows:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#protocol LocationControllerDelegate <NSObject>
#required
- (void)locationUpdate:(CLLocation*)location;
#end
#interface LocationController : NSObject <CLLocationManagerDelegate> {
__unsafe_unretained id <LocationControllerDelegate> _locDelegate;
CLLocationManager* locationManager;
CLLocation* location;
id locDelegate;
}
#property (nonatomic, retain) CLLocationManager* locationManager;
#property (nonatomic, retain) CLLocation* location;
#property (nonatomic, assign) id <LocationControllerDelegate> locDelegate;
+ (LocationController*)sharedInstance;
#end
My singleton implementation is as follows:
#import "MyLocation.h"
static LocationController* sharedCLDelegate = nil;
#implementation LocationController
#synthesize locationManager, location, locDelegate = _locDelegate;
- (id)init
{
self = [super init];
if (self != nil) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
return self;
}
- (void)dealloc
{
/* ... */
}
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation
{
/* ... */
}
- (void)locationManager:(CLLocationManager*)manager
didFailWithError:(NSError*)error
{
/* ... */
}
#pragma mark -
#pragma mark Singleton Object Methods
+ (LocationController*)sharedInstance {
#synchronized(self) {
if (sharedCLDelegate == nil) {
}
}
return sharedCLDelegate;
}
+ (id)allocWithZone:(NSZone *)zone {
#synchronized(self) {
if (sharedCLDelegate == nil) {
sharedCLDelegate = [super allocWithZone:zone];
return sharedCLDelegate;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
#end
What am I missing or doing wrong here?
I think the root of the problem is here:
#protocol LocationControllerDelegate <NSObject>
#required
- (void)locationUpdate:(CLLocation*)location;
#end
So you've defined a protocol with a required method, but unless I've missed it you haven't implemented that method anywhere in the object that adopts that protocol. The compiler is warning you that you haven't implemented all the required methods of the protocol.
Your singleton looks wrong. Your sharedInstance method should be:
+ (LocationController*)sharedInstance {
#synchronized(self) {
if (sharedCLDelegate == nil) {
sharedCLDelegate = [[self alloc] init];
}
}
return sharedCLDelegate;
}
And you can get rid of the allocWithZone: and copyWithZone: methods - they don't do anything useful.
That's most likely why your class isn't working, but probably has nothing to do with the "incomplete implementation" warning. The warning will be because you've forgotten to implement a method that's declared in your header or in one of your protocols, or maybe you've misspelled it. I couldn't spot it on a first pass, but I'll take another look.
If you double-click on the yellow warning icon in the header it should tell you which method hasn't been implemented properly.
By the way, the line below leaks because you're double-retaining (init sets retain count to one, and then assigning to a retained property sets it to 2). You should run the Analyze function, which will add blue warnings for leaks like this.
self.locationManager = [[CLLocationManager alloc] init];
Do one of the following instead:
//assign directly to the ivar so the setter method isn't used
locationManager = [[CLLocationManager alloc] init];
Or
//autorelease before assigning
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
locationUpdate is declared in your interface, but not defined in the implementation. locationUpdate: is defined -- close, but not quite.
Note: The message could have saved some time.