Create UIView programmatically without Interface Builder - objective-c

I try to create a view without IB and make it separated with its controller. The view is MediaPlayerView contains two buttons and the controller is MediaPlayerViewController. I called the view in controller and adding a Movie Player view in it. The buttons is showing but the movie view just dark screen. What's wrong with my code?
Here is my code:
MediaPlayerView.m
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIButton *audioFileButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *shortSoundButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[audioFileButton setFrame:CGRectMake(0, 40, 150, 40)];
[shortSoundButton setFrame:CGRectMake(0, 100, 200, 40)];
[audioFileButton setTitle:#"Play Audio File" forState:UIControlStateNormal];
[shortSoundButton setTitle:#"Play Short Sound" forState:UIControlStateNormal];
_viewController = [[MediaPlayerViewController alloc] init];
[audioFileButton addTarget:[self viewController]
action:#selector(playAudioFile:)
forControlEvents:UIControlEventTouchUpInside];
[shortSoundButton addTarget:[self viewController]
action:#selector(playShortSound:)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:audioFileButton];
[self addSubview:shortSoundButton];
}
return self;
}
In MediaPlayerViewController.m, here is my loadView and viewDidLoad:
loadView:
MediaPlayerView *mPlayer = [[MediaPlayerView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[mPlayer setBackgroundColor:[UIColor clearColor]];
[self setView:mPlayer];
[mPlayer release];
viewDidLoad:
[super viewDidLoad];
float halfHeight = [[self view] bounds].size.height / 2.0;
float width = [[self view] bounds].size.width;
[[[self moviePlayer] view] setFrame:CGRectMake(0, halfHeight, width, halfHeight)];
[[self view] addSubview:[[self moviePlayer] view]];
Forgive for my bad writing, cause I am a newbie either in here, Objective-C or english.

Related

Can't see a new subview

All I am trying to do is add a new view to the content view of my NSWindow instance. When I do the following I do not see the new view (which should be black and take up the entire window). What am I doing wrong?
(done in response to a button click)
NSRect frameRect = [self.window frame];
frameRect.origin = NSZeroPoint;
NSView *view = [[NSView alloc] initWithFrame:frameRect];
view.wantsLayer = YES;
view.layer.backgroundColor = [NSColor blackColor].CGColor;
[self.window.contentView addSubview:view];
I've set up a simple project with a push button inside a ViewController and got a warning for accessing self.window. When using self.view.windowthe warning went away and your provided code works as expected.
Updated code
NSRect frameRect = [self.view.window frame];
frameRect.origin = NSZeroPoint;
NSView *view = [[NSView alloc] initWithFrame:frameRect];
view.wantsLayer = YES;
view.layer.backgroundColor = [NSColor blackColor].CGColor;
[self.view.window.contentView addSubview:view];
Update
Assuming that you're using an instance of WindowController where you're adding a button programmatically, your code works as expected.
#implementation WindowController
- (void)windowDidLoad
{
[super windowDidLoad];
CGRect buttonRect = CGRectMake(self.window.frame.size.width / 2 - 50,
self.window.frame.size.height / 2,
100,
50);
NSButton *button = [[NSButton alloc] initWithFrame:NSRectFromCGRect(buttonRect)];
[button setTitle: #"Click me!"];
[button setTarget:self];
[button setAction:#selector(buttonPressed)];
[self.window.contentView addSubview:button];
}
- (void)buttonPressed
{
NSRect frameRect = [self.window frame];
frameRect.origin = NSZeroPoint;
NSView *view = [[NSView alloc] initWithFrame:frameRect];
view.wantsLayer = YES;
view.layer.backgroundColor = [NSColor blackColor].CGColor;
[self.window.contentView addSubview:view];
}
An instance of NSViewController ain't got a property of window - only NSWindowController has one.

is it Possible to pass parameter in UIView Class

I ve got a view controller . when i pass an extra object I m getting this error #No visible interface in the below function .could u help me out.below is the code.where i am passing an object of type photo ..
ItemImageView *itemImage = [[ItemImageView alloc]initWithFrame:CGRectMake(currentPhotoPositionX,0,pageButtonWidth,pageButtonHeight) andPhoto:photo];
#ItemImageview
- (id)initWithFrame:(CGRect)frame :andPhoto:(Photo *)photo
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//Create Activity Indicator
activityIndicator= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(225, 115, 30, 30)];
[activityIndicator setBackgroundColor:[UIColor clearColor]];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[self addSubview:activityIndicator];
//Create Label Comments
lblComments = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 100.0f, 60.0f, 43.0f)];
[lblComments setText:#"Comments"];
[lblComments setBackgroundColor:[UIColor clearColor]];
[self addSubview:lblComments];
//Create Label Likes
lblLikes = [[UILabel alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 60.0f, 43.0f)];
[lblLikes setText:#"Likes"];
[lblLikes setBackgroundColor:[UIColor clearColor]];
[self addSubview:lblLikes];
//Create Item Button
btnItem = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnItem addTarget:self
action:#selector(:)
forControlEvents:UIControlEventTouchUpInside];
[btnItem setBackgroundImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
btnItem.frame = CGRectMake(0.0f, 0.0f, 144.0f, 143.0f);
[self addSubview:btnItem];
}
return self;
}
Change it into:
- (id)initWithFrame:(CGRect)frame andPhoto:(Photo *)photo
And also remember to add it to your .h file.

Issues with UISplitViewController and modal UIViewControllers

I have a really strange UI glitch in my iPhone/iPad app. Because I wanted to find the cause of it, I created a new project with as little code as possible. The important code is below.
Basically, I have a UISplitViewController containing two UIViewController subclasses. When a button is tapped, the first one presents modally and as UIModalPresentationFormSheet a UIViewController subclass called Modal. In there, when a button is tapped, another UIViewController subclass called Text is presented, this time as UIModalPresentationFullScreen. In Text, there is a UITextView. When it is tapped, everything is alright, but when the iPad is rotated, I get this:
The white part is the Text view controller, the red part in the background is ViewControllerTwo.
Does anybody have any idea why this happens? And what I can do to fix it?
Here is the Project: MediaFire
Here is the relevant source code:
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewControllerOne *one = [[ViewControllerOne alloc] init];
ViewControllerTwo *two = [[ViewControllerTwo alloc] init];
UISplitViewController *split = [[UISplitViewController alloc] init];
split.viewControllers = [NSArray arrayWithObjects:one, two, nil];
self.window.rootViewController = split;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
// ViewControllerOne.m
#import "Modal.h"
#implementation ViewControllerOne
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(30, 30, 44, 44);
[button addTarget:self action:#selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonTapped
{
Modal *modalOne = [[Modal alloc] init];
modalOne.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalOne animated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Modal.m
#import "Text.h"
#implementation Modal
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonOne.frame = CGRectMake(30, 30, 44, 44);
buttonOne.tag = 1;
[buttonOne addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonTwo.frame = CGRectMake(30, 100, 44, 44);
buttonTwo.tag = 2;
[buttonTwo addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonOne];
[self.view addSubview:buttonTwo];
}
- (void)buttonTapped:(UIButton *)button
{
if (button.tag == 1)
{
Text *text = [[Text alloc] init];
text.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:text animated:YES];
}
else
{
[self dismissModalViewControllerAnimated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Text.m
#implementation Text
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
You may need to present the modal views from the UISplitViewController instead of it's subviews...
UISplitViewController *splitViewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] splitViewController];
[splitViewController presentModalViewController:modal animated:YES];

How does autoresizing mask work?

I've found a lot of information on autoresizing but none has been able to solve my problem. I have a ViewController (RAViewController) who calls a view in its loadView method like so:
- (void)loadView
{
// Set Navigation Bar Title
self.navigationItem.title = #"RA";
// Add Background view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Add RAView
RAView *rAView = [[RAView alloc] initWithFrame:self.view.frame Controller:self];
[self.view rAView];
}
The view it calls (RAView) looks like this:
- (id)initWithFrame:(CGRect)frame Controller:(RAViewController *)Controller
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.autoresizesSubviews = YES;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
controller = Controller;
// Draw Architecture View
[self drawArchitectureView];
}
return self;
}
-(void)drawArchitectureView {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50);
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:#"Overview" forState:UIControlStateNormal];
[self addSubview:button];
}
For some reason I can't get autoresizing mask to resize the button width when the device is landscape. Any help is greatly appreciated.
If you want it to keep the same height, but just stay fully centered, 10.0 from the left and right, try:
-(void)drawArchitectureView {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50);
button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:#"Overview" forState:UIControlStateNormal];
[self addSubview:button];
}

Controls created on runtime has no window. Popovers cannot be presented from a view which does not have a window

i have button on the view and call following void to open popover controller with image picker, no problem here popover come up with image picker etc.
when i create UIButton programmatically on viewDidLoad the buttons calls same void below however this time that throws an exception "Popovers cannot be presented from a view which does not have a window."
-(IBAction)pickupPhoto:(id)sender
{
CGRect rect = CGRectMake(100, 100, 1, 1); // [(UIScrollView *)sender frame];
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.popOver = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[popOver setDelegate:self];
[popOver presentPopoverFromRect:rect inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
[imgPicker release];
}
here i am creating buttons on same view buttons call the void.
-(void)addButtons
{
for (int i = 0; i < 10; i++) {
self.btn = [[UIButton alloc] initWithFrame:CGRectMake(i * 100, 0, 100, 80)];
[[self.btn layer] setBorderColor:RGB(245, 245, 245).CGColor];
[[self.btn layer] setBorderWidth:1.0f];
[[self.btn titleLabel] setFont:[UIFont fontWithName:#"Thonburi" size:70]];
[self.btn setTitle:doubleToString(i+1) forState:UIControlStateNormal];
[self.btn setTitleColor:RGB(245, 245, 245) forState:UIControlStateNormal];
[self.btn setTitle:doubleToString(i+1) forState:UIControlStateSelected];
[self.btn setTitleColor:RGB(125, 225, 225) forState:UIControlStateHighlighted];
[self.btn addTarget:[NMBPostUI alloc] action:#selector(pickupPhoto:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
}
}
i can see when i call -(void)addButtons, self.view.window returns nil for the buttons created at runtime that causes the problem. yes i should create buttons during init but there is no init call when you call view controller as -storyBoard- Page Sheet. how can i walk around this?
any ideas are welcome.
many thanks.
Try creating buttons in:
initWithNibName:bundle:
or
awakeFromNib
methods. Maybe this will work.