I've been watching a video that stated that UIAlertView
works only if UIKit.h has been imported. However, if I comment out the import statement in the header file:
//#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
the alert still works when I add it to the implementation file:
- (void)viewDidLoad
{
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
Please explain why this works? What is the true role of UIKit?
It's because it's probably already declared in your Prefix.pch
file, which looks something like this by default:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
UIKit.h
is just the file where the classes etc. are defined.
Without it, the compiler wouldn't know what UIAlertView
is.
However, in your case it probably works anyway, as the #import <UIKit/UIKit.h>
is usually included in your project's prefix file, which is included by default in your sources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With