I've made a project and implemented the folder project and now I've set up a subview which is displayed aboce the folder like on the picture. On the popup there's a image and I would like to do it like this that if I tap the image a new controller with the whole image will displayed. I've set up all correctly...However, I tried to change the subview etc. THIS Navigationcontroller never appears at the top of all "layers". it appears in the folderviewcontroller. By the way, the Navcontroller is set up by this project: https://github.com/mwaterfall/MWPhotoBrowser and the Popup was set up through this: https://github.com/kgn/KGModal
So here's my code:
-(IBAction)mehr:(id)sender {
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 440)];
cubut = [UIButton buttonWithType:UIButtonTypeCustom];
[cubut addTarget:self
action:#selector(dothis:)
forControlEvents:UIControlEventTouchUpInside];
[cubut setTitle:#"Show View" forState:UIControlStateNormal];
cubut.frame = CGRectMake(5, 70, 270, 200);
[contentView addSubview:cubut];
- (IBAction)dothis:(id)sender {
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set browser options.
browser.wantsFullScreenLayout = YES;
browser.displayActionButton = YES;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browser];
[self presentModalViewController:navController animated:YES];
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"star" ofType:#"png"]];
photo.caption = #"The star is soo beateful...";
[photos addObject:photo];
self.photos = photos;
}
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _photos.count;
}
And the picture:
(The button is set to custom so you cant see it...it has the same frame like the image)
So this was not finished so I have to awnser my own questions that I didnt found a solution for this.
Related
I have set up two barbutton items one on the right and one on the left. The right one successfully changes between two views, and the second one ont the right is a segmented control which is supposed to change between two views like wise. Here is my code, i want to know how to implement changing views with the segmented control.
- (void)setupNavigationBar {
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0f)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"ContainerView"];
navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(switchViewControllers)];
navBar.items = [NSArray arrayWithObject:navItem];
segmented = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"seg1.png"],
[UIImage imageNamed:#"seg1.png"],
nil]];
segmented.frame = CGRectMake(0, 0, 50, 30);
segmented.segmentedControlStyle = UISegmentedControlStyleBar;
UIBarButtonItem * segmentedBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmented];
navItem.leftBarButtonItem = segmentedBarItem;
[self.view addSubview:navBar];
}
- (void)viewDidLoad {
[super viewDidLoad];
// This is just for the purpose of calling the switch method.
[self setupNavigationBar];
CGRect rect = CGRectMake(0, 44, 320, 436);
currentViewController = [[FirstViewController alloc] init];
currentViewController.view.frame = rect;
nextViewController = [[SecondViewController alloc] init];
nextViewController.view.frame = rect;
[self addChildViewController:currentViewController];
[self addChildViewController:nextViewController];
[self.view addSubview:currentViewController.view];
}
- (void)switchViewControllers {
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:3.0 options:UIViewAnimationTransitionFlipFromRight | UIViewAnimationOptionCurveEaseInOut animations:nil completion:^(BOOL finished) {
UIViewController *tempViewController = currentViewController;
currentViewController = nextViewController;
nextViewController = tempViewController;
tempViewController = nil;
}];
}
How would I implement the segmented control to change views?
For add UISegmentedControl on UINavigationBar
EDIT :
NSArray *itemArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
nil];
UISegmentedControl* menuSegmentedButton = [[UISegmentedControl alloc] initWithItems:itemArray];
menuSegmentedButton.momentary = YES;
menuSegmentedButton.selected = NO;
menuSegmentedButton.segmentedControlStyle = UISegmentedControlStyleBar;
[menuSegmentedButton addTarget:self action:#selector(doMenu)
forControlEvents:UIControlEventValueChanged];
[menuSegmentedButton sizeToFit];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc]
initWithCustomView:menuSegmentedButton];
self.navigationItem.leftBarButtonItem = barButton;
Add this method for segment button tapped
-(void)doMenu
{
/// code for segment button tapped
}
I guess performance issues may require having separate view controllers rather than two subviews but it is much more complex - You would have a very complicated navigation system and would need to set the segment control for each view. I am sure there is another way to subclass a view but I am unfamiliar with this so would suggest having one view controller containing your views.
Note: If you were to programmatically create all the objects in each view, you could achieve great performance by removing objects from any hidden views.
So first you have a UISegmentedControl (as created in iPatel's answer). But add in this line [ctrl addTarget:self action:#selector(switchViewControllers) forControlEvents:UIControlEventValueChanged];
Then in switchViewControllers put:
if (ctrl.selectedSegmentIndex == 0) {
NSLog(#"One");
self.viewOne.hidden = NO;
self.viewTwo.hidden = YES;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"showView" forKey:#"viewOne"];
[defaults synchronize];
}
else if (ctrl.selectedSegmentIndex == 1) {
NSLog(#"Two");
}
-(void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:#"showView"] isEqualToString:#"viewOne"]) {
self.viewOne.hidden = NO;
self.viewTwo.hidden = YES;
}
else if (...) {
}
}
The above should work when you toggle the segment control. In each if you can toggle the visibility of two views. You would create the different views as a subview of the main view; one on top of the other. You would need to hide one view either in Interface Builder or in the viewDidLoad (I would suggest storing the current view in NSUserDefaults and showing that view when the user first loads the view controller).
I have set up a subview "popup" in my application and I want to show a navController if the user taps a button on the subview popup. I've set up the button so far, but if I tap the button the navigationController appears under my popup!? I've searched for some solution but I didn't found any. The whole controller is actually displayed in a folder which you can find here: https://github.com/jwilling/JWFolders So the viewDidLoad belong to the folder and the rootview.
I tried to make it as a subview of the popup but that doesn't work too. Does anyone know how to treat that? I've set up the popup programmaticaly and the navigationController too.
Thanks in advance.
My code:
The navController setup:
- (IBAction)dothis:(id)sender {
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set browser options.
browser.wantsFullScreenLayout = YES;
browser.displayActionButton = YES;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browser];
[self presentModalViewController:navController animated:YES];
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"star" ofType:#"png"]];
photo.caption = #"The star is soo beateful...";
[photos addObject:photo];
self.photos = photos;
}
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _photos.count;
}
The popup code:
-(IBAction)mehr:(id)sender {
//the popup size and content
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 440)];
CGRect welcomeLabelRect = contentView.bounds;
welcomeLabelRect.origin.y = 20;
welcomeLabelRect.size.height = 40;
UILabel *welcomeLabel = [[UILabel alloc] initWithFrame:welcomeLabelRect];
//an simple activityindicator
activityindi = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityindi.frame = CGRectMake(120, 200, 40, 40);
[activityindi startAnimating];
[contentView addSubview:activityindi];
//The Imageview
CGRect infoimagerect = CGRectMake(5, 70, 270, 200);
UIImageView *infoimage = [[UIImageView alloc] initWithFrame:infoimagerect];
//and the Button
cubut = [UIButton buttonWithType:UIButtonTypeCustom];
[cubut addTarget:self
action:#selector(dothis:)
forControlEvents:UIControlEventTouchUpInside];
[cubut setTitle:nil forState:UIControlStateNormal];
cubut.frame = CGRectMake(5, 70, 270, 200);
//retrieving data from parse.com
PFQuery *query = [PFQuery queryWithClassName:#"My-Application"];
[query getObjectInBackgroundWithId:#"My-ID"
block:^(PFObject *textdu, NSError *error) {
if (!error) {
//hide the Button if there is no image
cubut.hidden=YES;
//the headline of popup
UIFont *welcomeLabelFont = [UIFont fontWithName:#"copperplate" size:20];
welcomeLabel.text = [textdu objectForKey:#"header"];
welcomeLabel.font = welcomeLabelFont;
welcomeLabel.textColor = [UIColor whiteColor];
welcomeLabel.textAlignment = NSTextAlignmentCenter;
welcomeLabel.backgroundColor = [UIColor clearColor];
welcomeLabel.shadowColor = [UIColor blackColor];
welcomeLabel.shadowOffset = CGSizeMake(0, 1);
welcomeLabel.lineBreakMode = UILineBreakModeWordWrap;
welcomeLabel.numberOfLines = 2;
[contentView addSubview:welcomeLabel];
//the image from parse
if (!error) {
PFFile *imageFile = [textdu objectForKey:#"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
infoimage.image = image;
infoimage.contentMode = UIViewContentModeScaleAspectFit;
//show the button when the image appears
cubut.hidden = NO;
[contentView addSubview:infoimage];
//stop the activityindicator
[activityindi stopAnimating];
}
}];
}
} else {
//show some text
welcomeLabel.text = #"No connection!";
[welcomeLabel sizeToFit];
//hide the button
cubut.hidden = YES;
[contentView addSubview:infoLabel];
//stop the activityindicator
[activityindi stopAnimating];
}
}];
//add the content to the KNGModal view
[[KGModal sharedInstance] showWithContentView:contentView andAnimated:YES];
}
My viewDidLoad
- (void)viewDidLoad
{
but.hidden = YES;
PFQuery *query = [PFQuery queryWithClassName:#"myapp"];
[query getObjectInBackgroundWithId:#"Rgq5vankdf"
block:^(PFObject *textu, NSError *error) {
if (!error) {
but.hidden = NO;
but.color = [UIColor colorWithRed:0.90f green:0.90f blue:0.90f alpha:1.00f];
} else {
//if failure
but.hidden = YES;
mol.text = #"No Connection";
}
}];
[super viewDidLoad];
}
Pictures:
The button to open the folder:
The folder itself:
The popup:
Thanks in advance.
From the so far discussion and debugging the code you want to have the photo browser on the pop-up with a navigation controller.
So here is the sample code which implements this functionality, have a look at it.
I have used the same KGModal sample code and extended as per the requirement. I have used Xib to have a view with navigation bar.
To dismiss the pop-up from any where in the app you can use the below line, as it is shared instance.
[[KGModal sharedInstance] hideAnimated:YES];
Update:
The reason for showing the photo browser with in folderView is, you are trying to present the photoBrowser within the folderView, so it was presenting within the folderView of very small height & not able to see any photo.
So my suggestion is, as the user taps on pop-up to view photoBrowser you just remove pop-up and present the photoBrowser from the viewController class, as other than this class everything is handled through views.
I have made the changes as per above & works fine, to the code given by you, download the code here and have a look at it.
Let me know if it fulfills your needs.
Thanks
I noticed this line of code:
[[KGModal sharedInstance] showWithContentView: contentView andAnimated: YES];
And I can only think that, since it is a singleton, it adds the contentView on the UIApplication's key window. If that is the case, then a modal view controller will always be below the popup. You can solve this by adding a new method to the KGModal class
- (void) showWithContentView: (UIView*) contentView
inViewController: (UIViewController*) controller
andAnimated: (BOOL) animated;
the method should show the popup in the specified controller's view; you should use that method instead.
Edit
After some more digging, I found that KGModal displays the popup on another window. The quickest fix would be to dismiss the popup, then show the nav controller.
I am using a ABUnknownPersonViewController for displaying contact information within UINavigatonController and I want a button at the bottom which will push another viewController with additional information on that user. I have been able to add a button to the bottom and I can scroll down and view it, but it does not stay on screen.
I am looking for the same thing, but can you please show me how did you add a button at the bottom?
What I did is add a button at the top, inside the navigation bar, although it does have a few visual problems, this is how i did it.
//I created a UIToolBar
UIToolBar *myToolBar = [[UIToolBar alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
//then set the style to match my navBar
[myToolBar setBarStyle:UIBarStyleBlackOpaque];
//and finally created the button.
NSMutableArray *buttons = [NSMutableArray alloc] initWithCapacity:1]autorelease];
UIBarButtonItem *details = [[[UIBarButtonItem alloc] init]autorelease];
[details setTitle: #"details"];
//after that placed it on top of the UPVC
unknownPersonViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myToolBar];
I hope this helps you in some way...
- (void)loadView
{
[super loadView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Horray Button!!" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 500.0, 160.0, 40.0);
//[self.view addSubview:button];
NSArray *subviews = [self.view subviews];
NSLog(#"subviews: %d", [subviews count]);
UIScrollView *v = [subviews objectAtIndex:0];
[v addSubview:button];
CGSize cgSize = [v contentSize];
cgSize.height = 500.0 + 40.0;
[v setContentSize:cgSize];
}
The code I am using to add the button down at the bottom, but it does not stay in view once scrolled to.
I'm new to Objective C. I created the method to construct & display a button inside a UIView (UIView is inside another View named contentView, and contentView to be added as a subview in a UIScrollView. Well the problem is that I cannot click on the generated button to trigger the IB action named playAction. Can someone pls help me? Thanks
- (void) displayCategoryBestSellers
{
//Structure: ScrollView -> ContentView -> MainView -> ImageView and Button
UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0,0,160,105)];
mainView.userInteractionEnabled = YES;
UIImage * backgroundImage = [UIImage imageNamed:#"blackwhitesquare.png"];
UIImageView * uiImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,145,105)];
uiImageView.image = backgroundImage;
uiImageView.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2);
uiImageView.userInteractionEnabled = YES;
[mainView addSubview:uiImageView];
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(0,0,100,90);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://media.theseus.cataloguesolutions.com/images/72by72/324178611_0004_PG_1.jpg"]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[playButton setBackgroundImage:myimage forState:UIControlStateNormal];
playButton.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2);
[playButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchDown];
playButton.userInteractionEnabled = YES;
[mainView addSubview:playButton];
[contentView addSubview:mainView];
contentView.userInteractionEnabled = YES;
[scrollView addSubview:contentView];
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
}
-(IBAction)playAction: (id)sender
{
NSLog(#"Button Clicked");
}
Use UIControlEventTouchUpInside instead of UIControlEventTouchDown.
I Created Segment Control through Interface Builder.
Created a IBAction and Linked to Value Changed Option of segment Controller.
- (IBAction)GenderBttonAction:(id)sender {
printf("\n Segemt Controll");
}
When i click on segment controller this method is calling , but how would i get the seleced index value of segment controller.
Please help me dears.
((UISegmentedControl *)sender).selectedSegmentIndex;
:-)
I use the following code in a view controller.m to say launch a modal controller and it seems to work good for me.
- (void)viewDidLoad
{
NSArray *segmentContent = [NSArray arrayWithObjects:
NSLocalizedString(#"view 1", #""),
NSLocalizedString(#"view 2", #""),
NSLocalizedString(#"Close", #""),
nil];
//or images insted of text
//NSArray *segmentContent = [NSArray arrayWithObjects:
// [UIImage imageNamed:#"pic.png"],
// [UIImage imageNamed:#"spic.png"],
// [UIImage imageNamed:#"close.png"],
// nil]];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentContent];
segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment;
segmentedControl.momentary = YES; // option
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 160, 30);
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segments = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segments;
self.navigationItem.title = #"My title";
[segments release];
Then add the actions on selection, last one is a close statement if you launched a modal controller.
- (IBAction)segmentAction:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
switch ([sender selectedSegmentIndex])
{
case 0:
{
// Do stuff like launch a modal controller. But don't forget to add this all into your modal controller views to get back out :)
InfoViewController *infoViewController = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
UINavigationController *aInfoViewController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
[self presentModalViewController:aInfoViewController animated:YES];
break;
}
case 1:
{
// do stuff
break;
}
case 2:
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
break;
}
}
NSLog(#"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
}
Use the method below if needed to close the modal via this way.
- (IBAction)dismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
and don't forget to declare the action/method in the same respective h file.
- (IBAction)dismissAction:(id)sender;
Hope this helps.
Sincerely, Kirk