프로그래밍 방식으로 segue 만들기
나는 UIViewController
모든 UIViewsControllers
공통된 작업을 재사용하기 위해 확장한다는 공통점이 있습니다.
나는이 "공통"에 UIViewController
대해 다른 모든 것이 UIViewControllers
상속 되도록 세구를 설정하고 싶다 .
프로그래밍 방식으로 어떻게해야합니까?
질문은 스토리 보드에 가지 않고 손으로 직접하지 않고 어떻게 segue
모두를 위해 어떻게 설정할 수있을 것 같아요 UIViewControllers
.
정의상 segue는 스토리 보드와 독립적으로 존재할 수 없습니다. 클래스 이름에도 있습니다 UIStoryboardSegue
. 프로그래밍 방식으로 segue를 만들지 마십시오. 스토리 보드 런타임이 만들어집니다. 일반적으로 performSegueWithIdentifier:
뷰 컨트롤러의 코드를 호출 할 수 있지만, 스토리 보드에 이미 참조 용 segue가 설정되어 있어야합니다.
그래도 내가 생각하는 것은 공통 뷰 컨트롤러 (기본 클래스)에서 새 뷰 컨트롤러로 전환하고 모든 파생 클래스에서 상속되는 메서드를 만드는 방법입니다. 기본 클래스 뷰 컨트롤러에 이와 같은 메소드를 작성하여이를 수행 할 수 있습니다.
- (IBAction)pushMyNewViewController
{
MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
// do any setup you need for myNewVC
[self presentModalViewController:myNewVC animated:YES];
}
그런 다음 파생 클래스에서 적절한 버튼을 클릭하거나 테이블 행을 선택하거나 그 밖의 것을 선택할 때 해당 메소드를 호출하십시오.
나는 또 다른 가능성을 추가 할 것이라고 생각했다. 수행 할 수있는 작업 중 하나는 액션에 연결되지 않은 segue를 사용하여 스토리 보드에서 두 장면을 연결 한 다음 뷰 컨트롤러 내에서 segue를 프로그래밍 방식으로 트리거 할 수 있다는 것입니다. 이렇게하는 방법은 스토리 장면의 맨 아래에있는 파일의 소유자 아이콘에서 segueing scene 인 드래그하여 대상 장면으로 마우스를 끌어 야한다는 것입니다. 설명을 돕기 위해 이미지를 던질 것입니다.
"Manual Segue"팝업이 나타납니다. 유형으로 푸시를 선택했습니다. 작은 사각형을 탭하고 속성 관리자에 있는지 확인하십시오. 코드에서 참조 할 때 사용할 식별자를 제공하십시오.
좋아, 다음은 프로그래밍 방식의 바 버튼 항목을 사용하여 정리하겠습니다. viewDidLoad 또는 다른 곳에서 탐색 코드에 다음 코드를 사용하여 버튼 항목을 만듭니다.
UIBarButtonItem *buttonizeButton = [[UIBarButtonItem alloc] initWithTitle:@"Buttonize"
style:UIBarButtonItemStyleDone
target:self
action:@selector(buttonizeButtonTap:)];
self.navigationItem.rightBarButtonItems = @[buttonizeButton];
좋아, 선택기는 buttonizeButtonTap :입니다. 따라서 해당 버튼에 대해 void 메소드를 작성하고 해당 메소드 내에서 다음과 같이 segue를 호출합니다.
-(void)buttonizeButtonTap:(id)sender{
[self performSegueWithIdentifier:@"Associate" sender:sender];
}
prepareForSegue가 호출 될 때 버튼을 식별하려면 sender 매개 변수가 필요합니다. PreparingForSegue는 장면을 인스턴스화하고 작업에 필요한 값을 전달하는 프레임 워크 방법입니다. 내 방법은 다음과 같습니다.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"Associate"])
{
TranslationQuizAssociateVC *translationQuizAssociateVC = [segue destinationViewController];
translationQuizAssociateVC.nodeID = self.nodeID; //--pass nodeID from ViewNodeViewController
translationQuizAssociateVC.contentID = self.contentID;
translationQuizAssociateVC.index = self.index;
translationQuizAssociateVC.content = self.content;
}
}
좋아, 방금 테스트했고 작동합니다. 그것이 당신을 돕기를 바랍니다.
이 코드를 사용하여 사용자 정의 segue 서브 클래스를 인스턴스화하고 프로그래밍 방식으로 실행했습니다. 작동하는 것 같습니다. 이것에 문제가 있습니까? 나는 할 수 없다는 다른 모든 답변을 읽고 의아해합니다.
UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"];
MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController];
[self prepareForSegue:segue sender:sender];
[segue perform];
이것이 답변되고 받아 들여지는 것 같지만 몇 가지 세부 정보를 추가하고 싶습니다.
What I did to solve a problem where I would present a login-view as first screen and then wanted to segue to the application if login were correct. I created the segue from the login-view controller to the root view controller and gave it an identifier like "myidentifier".
Then after checking all login code if the login were correct I'd call
[self performSegueWithIdentifier: @"myidentifier" sender: self];
My biggest misunderstanding were that I tried to put the segue on a button and kind of interrupt the segue once it were found.
You have to link your code to the UIStoryboard
that you're using. Make sure you go into YourViewController in your UIStoryboard
, click on the border around it, and then set its identifier
field to a NSString
that you call in your code.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
YourViewController *yourViewController =
(YourViewController *)
[storyboard instantiateViewControllerWithIdentifier:@"yourViewControllerID"];
[self.navigationController pushViewController:yourViewController animated:YES];
For controllers that are in the storyboard.
jhilgert00 is this what you were looking for?
-(IBAction)nav_goHome:(id)sender {
UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
[self.navigationController pushViewController: myController animated:YES];
}
OR...
[self performSegueWithIdentifier:@"loginMainSegue" sender:self];
well , you can create and also can subclass the UIStoryBoardSegue . subclassing is mostly used for giving custom transition animation.
you can see video of wwdc 2011 introducing StoryBoard. its available in youtube also.
I'd like to add a clarification...
A common misunderstanding, in fact one that I had for some time, is that a storyboard segue is triggered by the prepareForSegue:sender:
method. It is not. A storyboard segue will perform, regardless of whether you have implemented a prepareForSegue:sender:
method for that (departing from) view controller.
I learnt this from Paul Hegarty's excellent iTunesU lectures. My apologies but unfortunately cannot remember which lecture.
If you connect a segue between two view controllers in a storyboard, but do not implement a prepareForSegue:sender:
method, the segue will still segue to the target view controller. It will however segue to that view controller unprepared.
Hope this helps.
Storyboard Segues are not to be created outside of the storyboard. You will need to wire it up, despite the drawbacks.
UIStoryboardSegue Reference clearly states:
You do not create segue objects directly. Instead, the storyboard runtime creates them when it must perform a segue between two view controllers. You can still initiate a segue programmatically using the performSegueWithIdentifier:sender: method of UIViewController if you want. You might do so to initiate a segue from a source that was added programmatically and therefore not available in Interface Builder.
You can still programmatically tell the storyboard to present a view controller using a segue using presentModalViewController:
or pushViewController:animated:
calls, but you'll need a storyboard instance.
You can call UIStoryboard
s class method to get a named storyboard with bundle nil for the main bundle.
storyboardWithName:bundle:
First of, suppose you have two different views in storyboard, and you want to navigate from one screen to another, so follow this steps:
1). Define all your views with class file and also storyboard id in identity inspector.
2). Make sure you add a navigation controller to the first view. Select it in the Storyboard and then Editor >Embed In > Navigation Controller
3). In your first class, import the "secondClass.h"
#import "ViewController.h
#import "secondController.h"
4). Add this command in the IBAction that has to perform the segue
secondController *next=[self.storyboard instantiateViewControllerWithIdentifier:@"second"];
[self.navigationController pushViewController:next animated:YES];
5). @"second"
is secondview controller class, storyboard id.
I reverse-engineered and made an open source (re)implementation of UIStoryboard's segues: https://github.com/acoomans/Segway
With that library, you can define segues programmatically (without any storyboard).
Hope it may help.
A couple of problems, actually:
First, in that project you uploaded for us, the segue does not bear the "segue1" identifier:
no identifier
해당 식별자를 아직 입력하지 않은 경우 입력해야합니다.
둘째, 테이블보기에서 테이블보기로 푸시 할 때 initWithNibName을 호출하여보기 컨트롤러를 만듭니다. 실제로 instantiateViewControllerWithIdentifier를 사용하고 싶습니다.
다음에 대한 코드 샘플이 있습니다 Creating a segue programmatically
.
class ViewController: UIViewController {
...
// 1. Define the Segue
private var commonSegue: UIStoryboardSegue!
...
override func viewDidLoad() {
...
// 2. Initialize the Segue
self.commonSegue = UIStoryboardSegue(identifier: "CommonSegue", source: ..., destination: ...) {
self.commonSegue.source.showDetailViewController(self.commonSegue.destination, sender: self)
}
...
}
...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// 4. Prepare to perform the Segue
if self.commonSegue == segue {
...
}
...
}
...
func actionFunction() {
// 3. Perform the Segue
self.prepare(for: self.commonSegue, sender: self)
self.commonSegue.perform()
}
...
}
참고 URL : https://stackoverflow.com/questions/9674685/creating-a-segue-programmatically
'Programming' 카테고리의 다른 글
프로그래밍 방식으로 EditText의 inputType을 설정 하시겠습니까? (0) | 2020.05.07 |
---|---|
요청 실패 : 허용되지 않는 콘텐츠 유형 : AFNetworking 2.0을 사용하는 text / html (0) | 2020.05.07 |
Powershell에서 파일의 압축을 푸는 방법은 무엇입니까? (0) | 2020.05.07 |
Eclipse 오류 : 'Java Virtual Machine을 작성하지 못했습니다' (0) | 2020.05.06 |
폴더의 파일 이름을 순차 번호로 바꾸기 (0) | 2020.05.06 |