How to load view from the UIView Controller? Clarification needed - uiview

I know this is very basic, but after massive refactoring, i remain a bit confused.
I have a scene created with the help of a storyboard. When a button on a main menu screen is tapped, a GameView should load.
GameView has a controller GameViewController
When the program runs, the GameViewController is called that in turn
- (void) viewWillAppear:(BOOL)animated {
[self setGameView:[[GameView alloc] initWithFrame
:CGRectMake(0, 0, 300, 480)]];
[self setView:[self gameView]];
[self loadView];
}
What shows on the simulator, however is not a GameView, but a mock up created via the Storyboard.
What am i missing please?
I also tried the following in UIViewController to no avail
UIView *gameView = [[GameView alloc] initWithFrame:CGRectMake(0, 0, 300, 480)];
[[[self view] superview] addSubview:gameView];

Consider
UIView *gameView = [[GameView alloc] initWithFrame:CGRectMake(0, 0, 300, 480)];
[[self view] addSubview:gameView];

Related

Add subview programmatically using instance method

I am trying to add a subview programmatically to my ViewController : UIViewController by using a method declared in my ThirdClass : NSObject class. Here is my code:
In the ViewController.m file I do:
- (void)viewDidLoad {
[super viewDidLoad];
ThirdClass *instanceOfThirdClass = [[ThirdClass alloc] init];
[instanceOfThirdClass createView];
}
And in my ThirdClass.m I declare the instance method:
-(void)createView{
NSLog(#"enter create app");
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 0, 320, 100)];
[myView setBackgroundColor:[UIColor redColor]];
ViewController *instanceOfViewController = [[ViewController alloc]init];
[instanceOfViewController.view addSubview:myView];
}
so the problem apparently is that I was trying to add the created view to the class instance, the correct way to do it is the one posted by #gary-riches below,
You are attaching the view you create to a new instantiated view controller, but the view you are displaying is that of the one belonging ViewController.m. You need to add the view to the ViewController.m's view.
Update your createView method to handle a view:
-(void)createViewInView:(UIView *)aView{
NSLog(#"enter create app");
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 0, 320, 100)];
[myView setBackgroundColor:[UIColor redColor]];
[aView addSubview:myView];
}
Then change your call to be:
[instanceOfThirdClass createViewInView:self.view];
Also, make sure you have the method signature in the header of ThirdClass.h. It should be:
-(void)createViewInView:(UIView *)aView;

popover size issue with ios 7.0.X, its not getting while loading popover

My popover is not getting properly sized while presenting with ios 7.03.
I'm Trying like
{
...
ClassObj *mail = [...];
[mail.view setFrame:CGRectMake(0, 0, 320, 216)];
[mail setContentSizeForViewInPopover:CGSizeMake(320,256)];
[refinePopover presentPopoverFromRect:sender.frame inView:[sender superview]
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[mail setContentSizeForViewInPopover:CGSizeMake(320,256)];
...
}
And in ClassObj class i'm setting frame and size in viewwill/didApear method
{
[self setContentSizeForViewInPopover:CGSizeMake(320,256)];
[self.view setFrame:CGRectMake(0, 0, 320, 216)];
}
I'm plying more on this please let me know if you have any suggestion, Thanks in Advance. :)
You create a property of UIPopOverController Object in your ClassObj
and then assign that popoverController from that class where you are creating Your actual popover.
mail.popoverController = refinePopover;
now in your ClassObj in viewwill/didApear method add this code with 0.1 delay.
[popoverController setPopoverContentSize:CGSizeMake(320, 256)];
hope this will help you.

in UIView subclass, selector not working when used twice in the same view - IOS

This is my custom view for video player.
Interface File : Movieplayer.h
#import <UIKit/UIKit.h>
#interface MoviePlayer : UIView{
}
#property (strong) UIButton *videoButton;
#end
Implementation File: Movieplayer.m
#import "MoviePlayer.h"
#import <MediaPlayer/MediaPlayer.h>
#implementation MoviePlayer
MPMoviePlayerViewController *movieController;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_videoButton = [[UIButton alloc] initWithFrame:self.frame];
NSLog(#"My view frame: %#", NSStringFromCGRect(self.frame));
[_videoButton setBackgroundImage:[UIImage imageNamed:#"video-default.jpg"] forState:UIControlStateNormal];
[_videoButton addTarget:self action:#selector(showPlayer:) forControlEvents:UIControlEventTouchUpInside];
int current_tag = rand();
[_videoButton setTag:current_tag];
NSLog(#"Current tag : %d",current_tag);
[self addSubview:_videoButton];
}
return self;
}
-(void) showPlayer : (UIButton *) sender {
NSURL *movieURL = [NSURL URLWithString:#"http://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1211/sample_iTunes.mov"];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[movieController.view setFrame:_videoButton.frame];
[movieController.moviePlayer play];
[self addSubview:movieController.view];
}
#end
When i am using this class in viewcontroller, it works fully on the first control. But if i add 2 instance of the same class, the button selector is not fired for the second one.
here is how i am using it,
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrolview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
// Do any additional setup after loading the view, typically from a nib.
MoviePlayer *mplayer = [[MoviePlayer alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
[scrolview addSubview:mplayer];
MoviePlayer *mplayer2 = [[MoviePlayer alloc] initWithFrame:CGRectMake(0, 180, 320, 180)];
[scrolview addSubview:mplayer2];
[self.view addSubview:scrolview];
[scrolview setContentSize:CGSizeMake(320, 600)];
}
When i click on mplayer1, it plays the video, all works here. When i click on mplayer2 nothing happens. in mplayer2, it does not call Showplayer method.
Please help.
Change the frame of the button by below way.
_videoButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,frame.size.width, frame.size.height)];
For second MoviePlayer view, the frame starts with origin.y = 180. So your button will be placed outside of your view. Change the button frame as above to solve the issue.
The problem you're having is that the _videoButton is not actually inside the bounds of its parent MoviePlayer view. In the init function, you say:
_videoButton = [[UIButton alloc] initWithFrame:self.frame];
Since the _videoButton's frame is relative to its superview, this means for your section MoviePlayer, the _videoButton gets a frame with a y value of 180. This means it is 180 points below the top of the MoviePlayer. The MoviePlayer, though, is only 180 points tall, itself, so the _videoButton is entirely outside of its parent view. This prevents you from tapping on it, even though it's visible. (By default, views don't hide subviews if they are outside of their bounds.) You can verify this by giving your MoviePlayer view a background color.
The fix is easy: don't pass your own frame to any child views. You probably instead want to do something like:
_videoButton = [[UIButton alloc] initWithFrame:self.bounds];
If you want the child view to take up the entire parent view. (Or, even better, use a nib or autolayout constraints to avoid these kinds of issues.)

UIScrollView won't scroll inside as subview

I have a UIViewController and a UIScrollView. Since I have to add multiple views to the view controller, and the scroll view is just one of them, I tried to set the view of the controller to an dummy UIView and the scroll as child, like this:
[self setView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]];
[[self view] setUserInteractionEnabled:NO];
// TDHexMapScrollView inherits from UIViewController
[self setHexMapScrollView:[[TDHexMapScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]];
[[self view] addSubview:[self hexMapScrollView]];
This way the scrolling doesn't work. Adding it as the main view makes scrolling and panning work correctly:
[self setHexMapScrollView:[[TDHexMapScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]];
[self setView:[self hexMapScrollView]];
Any help? Thanks
Don't do this:
[[self view] setUserInteractionEnabled:NO];
That disables interaction for the view and all subviews. That means it ignores touch events and does not propagate them to its subviews, which means your scrollview doesn't get any events.
I didn't get reply on my comment, but I figured I would show you an example of how I go about doing this.
Declare
CGSize scrollSize;
In your implementation. Then create your scrollView, views, labels, etc.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenFrame.size.width, screenFrame.size.height - self.tabBarController.tabBar.frame.size.height - self.navigationController.navigationBar.frame.size.height)];
self.scrollView.scrollEnabled = YES;
scrollSize.height = self.View1.frame.size.height + self.Label1.frame.size.height + self.Label2.frame.size.height + self.Label3.frame.size.height + self.Label4.frame.size.height + self.Label5.frame.size.height + self.Label6.frame.size.height;
scrollSize.width = screenFrame.size.width;
[self.scrollView setContentSize:scrollSize];
Add to the scrollView
[self.scrollView addSubview:self.View1];
//Repeat for all views / labels / etc
Add the scrollView to the ViewController
[self.view addSubview:self.scrollView];

zooming animation problem in ScrollView - Ipad

In my app, I have a split screen in which the detail view is a scrollview. I have 5 tables which are subviews of my scrollview in which 3 table views are side by side on top and 2 table views are side by side on bottom
I have already implemented a way in which when I click any of the rows of any of the table in the scrollview, that view disappears and another view zooms into its position.
I write the following code in the didSelectRowAtIndexPath in the middle table subview,
CGFloat xpos = self.view.frame.origin.x;
CGFloat ypos = self.view.frame.origin.y;
self.view.frame = CGRectMake(xpos+100,ypos+150,5,5);
[UIView beginAnimations:#"Zoom" context:NULL];
[UIView setAnimationDuration:2];
self.view.frame = CGRectMake(xpos,ypos,220,310);
[UIView commitAnimations];
[self.view addSubview:popContents.view];
popContents is the view I need to zoom into to the view previously occupied by that particular table view and that happens correctly.
However the problem that I am facing is that since there is another table subview in the side, if I increase the frame size to say 250 or so, the part of the zoomed in view gets hidden by the tableview on the side ( as its as if a part of the zoomed in view goes under the tableview on the side).
Is there anyway to correct this so that my zoomed in view would not get hidden by the tableviews on its sides?
I hope I have explained my problem correctly...
UPDATE:
Here is the code I am using for adding the subviews for the scrollview
// Scroll view
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 1000, 740)];
scrollView.contentSize = CGSizeMake(1000, 700);
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
[self.view addSubview:scrollView];
aView = [[aViewController alloc] initWithNibName:#"aViewController" bundle:nil];
aView.view.frame = CGRectMake(10, 25, 220, 310);
[aView loadList:objPatients];
[scrollView addSubview:aView.view];
bView = [[bViewController alloc] initWithNibName:#"bViewController" bundle:nil];
bView.view.frame = CGRectMake(10, 350, 220, 310);
[bView loadList:objPatients];
[scrollView addSubview:bView.view];
cView = [[cViewController alloc] initWithNibName:#"cViewController" bundle:nil];
cView.view.frame = CGRectMake(240, 25, 220, 310);
[cView loadList:objPatients];
[scrollView addSubview:cView.view];
dView = [[dViewController alloc] initWithNibName:#"dViewController" bundle:nil];
enView.view.frame = CGRectMake(240, 350, 220, 310);
[enView loadList:objPatients];
[scrollView addSubview:dView.view];
eView = [[eViewController alloc] initWithNibName:#"eViewController" bundle:nil];
eView.view.frame = CGRectMake(470, 25, 220, 310);
[eView loadList:objPatients];
[scrollView addSubview:eView.view];
say for example, I add the code for didSelectRowAtIndexPath in cViewController subview...
This is a guess since I would need to know how your table views are added to the scroll view, but the middle table view was probably added before the one on the side. Views are "stacked" in the order they're added with the last one on top. You'll need to get the scroll view to move the middle view to the front with this method
- (void)bringSubviewToFront:(UIView *)view
The best way to do that would be to create a protocol for the table views and make the scroll view the delegate. The method would be something like this
- (void) moveAViewToFront: (MyTableView *) aTableView
{
[self.view bringSubviewToFront: aTableView.view];
}
You would then call the delegate method before setting up the animation.
Edited
After a little more thought I realized that the subviews have a reference to their superview so this bit of code should provide an idea on how to solve the problem. I created a test app which has a view controller which adds two sub views. The view controller header file is MoveSubviewViewController.h
#import <UIKit/UIKit.h>
#interface MoveSubviewViewController : UIViewController
{
}
#end
and it's implementation is
#import "MoveSubviewViewController.h"
#import "MoveableSubview.h"
#implementation MoveSubviewViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Create two overlapping subviews. The blue subview will start at the top of
// the frame and extend down two thirds of the frame.
CGRect superviewFrame = self.view.frame;
CGRect view1Frame = CGRectMake( superviewFrame.origin.x, superviewFrame.origin.y,
superviewFrame.size.width, superviewFrame.size.height * 2 / 3);
MoveableSubview *view1 = [[MoveableSubview alloc] initWithFrame: view1Frame];
view1.backgroundColor = [UIColor blueColor];
[self.view addSubview: view1];
[view1 release];
// The green subview will start one third of the way down the frame and
// extend all the to the bottom.
CGRect view2Frame = CGRectMake( superviewFrame.origin.x,
superviewFrame.origin.y + superviewFrame.size.height / 3,
superviewFrame.size.width, superviewFrame.size.height * 2 / 3);
MoveableSubview *view2 = [[MoveableSubview alloc] initWithFrame: view2Frame];
view2.backgroundColor = [UIColor greenColor];
[self.view addSubview: view2];
[view2 release];
}
#end
The subview class is MoveableSubview with another simple header
#import <UIKit/UIKit.h>
#interface MoveableSubview : UIView
{
}
#end
and implementation
#import "MoveableSubview.h"
#implementation MoveableSubview
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Move this view to the front in the superview.
[self.superview bringSubviewToFront: self];
}
#end
The thing to do is to add the
[self.superview bringSubviewToFront: self];
line before setting up the animation.

Resources