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?
UIKit.his just the file where the classes etc. are defined.Without it, the compiler wouldn’t know what
UIAlertViewis.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.